验证码图片 —— google kaptcha
整理普通登录验证码图片。
使用的是 google kaptcha。
依赖引用:
<dependencies>
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
GoogleCaptcha 配置验证码相关参数信息
@Configuration
public class GoogleCaptcha {
@Bean
public DefaultKaptcha getDefaultKaptcha() {
//验证码生成器
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
//配置
Properties properties = new Properties();
//是否有边框
properties.setProperty("kaptcha.border", "yes");
//设置边框颜色
properties.setProperty("kaptcha.border.color", "105,179,90");
//边框粗细度,默认为1
// properties.setProperty("kaptcha.border.thickness","1");
//验证码
properties.setProperty("kaptcha.session.key", "code");
//验证码文本字符颜色 默认为黑色
properties.setProperty("kaptcha.textproducer.font.color", "blue");
//设置字体样式
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅 黑");
//字体大小,默认40
properties.setProperty("kaptcha.textproducer.font.size", "30");
//验证码文本字符内容范围 默认为abced2345678gfynmnpwx
// properties.setProperty("kaptcha.textproducer.char.string", "");
//字符长度,默认为5
properties.setProperty("kaptcha.textproducer.char.length", "4");
//字符间距 默认为2
properties.setProperty("kaptcha.textproducer.char.space", "4");
//验证码图片宽度 默认为200
properties.setProperty("kaptcha.image.width", "100");
//验证码图片高度 默认为40
properties.setProperty("kaptcha.image.height", "40");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
上面的配置应该能满足普通的图片验证码需求,下面就需要使用 Controller 来完成请求返回验证码图片。
CaptchaController
@RestController
public class CaptchaController {
@Autowired
private DefaultKaptcha defaultKaptcha;
/**
*
* @param request
* @param response 对于 response 的输出做一些处理。
*/
@GetMapping(value = "/googleCaptcha", produces = "image/jpeg")
public void getGoogleCaptcha(HttpServletRequest request, HttpServletResponse response){
ServletOutputStream outputStream = null;
//定义response输出类型为image/jpeg
try {
setResponse(response);
//---------------------------生成验证码 begin----------------------
//获取验证码文本内容
String text = defaultKaptcha.createText();
System.out.println("验证码: " + text);
//将验证码放到session中,用于前端表单提交,后端从 Session 中判断验证码是否一致。
// 同时 如果不将验证码存入 Session 可以存入 redis 中。
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,text);
//根据文本内容创建图形验证码
BufferedImage image = defaultKaptcha.createImage(text);
outputStream = response.getOutputStream();
//输出流输出图片,格式为jpg
ImageIO.write(image,"jpg",outputStream);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
// 关闭流,释放资源
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//---------------------------生成验证码 end----------------------
}
/**
* 设置 response 的一些信息。
*/
private void setResponse(HttpServletResponse response){
response.setDateHeader("Expires",0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
}
}
测试结果:
小结
针对上面的代码只做基本的图片验证码的笔记。
关于更细的需求,例如:验证码过期时间、有效时间、存在 redis 中还是 session 中、请求间隔等等就需要自行实现或者查阅相关资料博客。