Java graphics verification code, support gif, Chinese and English, arithmetic
Preface
There are references to Redis in the article, please refer to https://blog.csdn.net/javanbme/article/details/114645759
The code in the general Response will not be posted. Remove the returned string by yourself or replace it with the one in your own project.
Integration example
1. Dependence
<!-- 图形验证码 -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
2. Core tools
public class CaptchaUtil {
private static SpecCaptcha specCaptcha = new SpecCaptcha(130, 48);
private static GifCaptcha gifCaptcha = new GifCaptcha(130, 48);
private static ChineseCaptcha chineseCaptcha = new ChineseCaptcha(130, 48);
private static ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha(130, 48);
private static ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(130, 48);
/**
* TODO 释义
* SpecCaptcha 和 GifCaptcha 可设置
* TYPE_DEFAULT 数字和字母混合
* TYPE_ONLY_NUMBER 纯数字
* TYPE_ONLY_CHAR 纯字母
* TYPE_ONLY_UPPER 纯大写字母
* TYPE_ONLY_LOWER 纯小写字母
* TYPE_NUM_AND_UPPER 数字和大写字母
*
* 内置字体类型 中文方式勿设置
* Captcha.FONT_1
* Captcha.FONT_2
* Captcha.FONT_3
* Captcha.FONT_4
* Captcha.FONT_5
* Captcha.FONT_6
* Captcha.FONT_7
* Captcha.FONT_8
* Captcha.FONT_9
* Captcha.FONT_10
*/
/**
* 英文
*/
public static SpecCaptcha specCaptcha() throws Exception{
//默认4位
specCaptcha.setLen(6);
specCaptcha.setCharType(Captcha.TYPE_ONLY_CHAR);
specCaptcha.setFont(Captcha.FONT_2);
return specCaptcha;
}
/**
* Gif
*/
public static GifCaptcha gifCaptcha() throws Exception{
gifCaptcha.setCharType(Captcha.TYPE_ONLY_CHAR);
gifCaptcha.setFont(Captcha.FONT_2);
return gifCaptcha;
}
/**
* 中文
*/
public static ChineseCaptcha chineseCaptcha() throws Exception{
return chineseCaptcha;
}
/**
* 中文Gif
*/
public static ChineseGifCaptcha chineseGifCaptcha() throws Exception{
return chineseGifCaptcha;
}
/**
* 算术
*/
public static ArithmeticCaptcha arithmeticCaptcha() throws Exception{
arithmeticCaptcha.setCharType(Captcha.TYPE_ONLY_CHAR);
arithmeticCaptcha.setFont(Captcha.FONT_2);
//几位数运算
arithmeticCaptcha.setLen(3);
return arithmeticCaptcha;
}
/**
* 输出至本地
*/
public static void main(String[] args) throws Exception{
FileOutputStream outputStream = new FileOutputStream(new File("/data/file/captcha.png"));
specCaptcha().out(outputStream);
outputStream.close();
//输出结果
System.out.println(specCaptcha.text());
}
}
3. controller
@RestController
public class LoginController {
@Autowired
private RedisUtils redisUtils;
/**
* 获取图形验证码
*/
@GetMapping("/captcha")
public void getCaptcha(HttpServletResponse response,String token) throws Exception{
SpecCaptcha specCaptcha = CaptchaUtil.specCaptcha();
specCaptcha.out(response.getOutputStream());
String verCode = specCaptcha.text().toLowerCase();
//5分钟过期
redisUtils.set(token, verCode, 5L * 60);
response.getOutputStream().close();
}
/**
* 登录校验示例
*/
@GetMapping("/login")
public Response login(String username,String password,String verCode,String verKey){
// 获取redis中的验证码
Object redisCode = redisUtils.get(verKey);
// 判断验证码是否过期
if (redisCode == null){
return Response.build().err(Error.CAPTCHA_KEY_ERROR);
}
if (StringUtils.isEmpty(verCode) || !redisCode.toString().equals(verCode.trim().toLowerCase())) {
return Response.build().err(Error.CAPTCHA_KEY_VALUE_ERROR);
}
// todo 登录业务
return Response.build().ok();
}
}
4. Effect
5. Specific instructions
1. Modify the redis address
2. Start the application
3. Access the verification code interface http://localhost:8080/captcha?token=abc The token value can be customized
4. Access the login interface http://localhost:8080/login?verKey= abc&verCode=kgsudm
vekey is the value of the above token verCode is the value of the graphic verification code, modify and debug by yourself
6. Source code