JavaWeb圖形驗證碼,支持gif驗證碼,可用於基於的session的web項目和先後端分離的項目。git
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>github
@RequestMapping("/captcha")
public void captcha(HttpServletResponse response) throws IOException {
// png類型
SpecCaptcha captcha = new SpecCaptcha(130, 48);
String text = captcha.text();// 獲取驗證碼的字符
char[] chars = captcha.textChar();// 獲取驗證碼的字符數組
System.out.println("驗證碼:"+text);
System.out.println(chars);
// 輸出驗證碼
captcha.out(response.getOutputStream());
}web
@RequestMapping("/captcha")
public void captcha(HttpServletResponse response) throws IOException {
// 三個參數分別爲寬、高、位數
GifCaptcha gifCaptcha = new GifCaptcha(100, 48, 4);
// 設置類型:字母數字混合
gifCaptcha.setCharType(Captcha.TYPE_DEFAULT);
//獲取驗證碼
String text = gifCaptcha.text();
System.out.println("驗證碼爲:"+text);
// 輸出驗證碼
gifCaptcha.out(response.getOutputStream());
}後端
@RequestMapping("/captcha")數組
public void captcha(HttpServletResponse response) throws IOException {
// 中文類型
ChineseCaptcha captcha = new ChineseCaptcha(130, 48);
//獲取驗證碼
String text = captcha.text();
System.out.println("驗證碼爲:"+text);
// 輸出驗證碼
captcha.out(response.getOutputStream());
}session
@RequestMapping("/captcha")app
public void captcha(HttpServletResponse response) throws IOException {
// 算術類型
ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
captcha.setLen(3); // 幾位數運算,默認是兩位
captcha.getArithmeticString(); // 獲取運算的公式
String text = captcha.text();// 獲取運算的結果
System.out.println("計算結果爲:"+text);
// 輸出驗證碼
captcha.out(response.getOutputStream());
}前後端分離