验证码的工具类
public class ValidateCodeUtil {
private DefaultKaptcha defaultKaptcha;
private String createText;
public ValidateCodeUtil(DefaultKaptcha defaultKaptcha) {
this.defaultKaptcha = defaultKaptcha;
}
public String createText() {
this.createText = defaultKaptcha.createText();
return createText;
}
public void writeToPage(HttpServletResponse httpServletResponse)throws Exception {
byte[] captchaChallengeAsJpeg = null;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
// 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
BufferedImage challenge = defaultKaptcha.createImage(createText);
ImageIO.write(challenge, "jpg", jpegOutputStream);
// 定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
}
}
配置文件config
@Component
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha() {
//创建对象
com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
//创建配置文件对象
Properties properties = new Properties();
// 图片边框
properties.setProperty("kaptcha.border", "yes");
// 边框颜色
properties.setProperty("kaptcha.border.color", "105,179,90");
// 字体颜色
properties.setProperty("kaptcha.textproducer.font.color", "red");
// 图片宽
properties.setProperty("kaptcha.image.width", "110");
// 图片高
properties.setProperty("kaptcha.image.height", "40");
// 字体大小
properties.setProperty("kaptcha.textproducer.font.size", "30");
// session key
properties.setProperty("kaptcha.session.key", "code");
// 验证码长度
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 字体
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
依赖
<!-- 验证码处理
https://mvnrepository.com/artifact/com.github.penggle/kaptcha -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
jsp代码
<script type="text/javascript">
var isVerifyCode=false;//验证码是否验证通过
$(function(){
//录入验证码时,到后台进行验证
$("#tryCode").blur(function(){
var tryCode=$("[name='tryCode']").val();
$.post(
"validateDefaultKaptcha",
{tryCode:tryCode},
function(obj){
if(obj){
isVerifyCode=true;//验证码通过
}else{
alert("验证码错误,重新输入");
isVerifyCode=false;
}
},
"json"
);
})
})
</script>
<body>
<form>
用户:<input type="text" name="uname" onblur="isExist()"><br>
密码:<input type="password" name="pwd" id="pwd"><br>
确认密码:<input type="password" name="pwd1" id="pwd1"><br>
*******验证码:<input type="text" id="tryCode" name="tryCode">*******************************
*********<img id="defaultKaptcha" src="/defaultKaptcha" onclick="this.src='/defaultKaptcha'"><br>
<input type="button" value="注册" onclick="reg()">
</form>
</body>
controller层
@Autowired
private DefaultKaptcha defaultKaptcha;
String VerifyCode = "VerifyCode";
//生成验证码
@RequestMapping("/defaultKaptcha")
public void defaultKaptcha(HttpServletRequest request,HttpServletResponse response) throws Exception {
//创建验证码工具包
ValidateCodeUtil validateCodeUtil = new ValidateCodeUtil(defaultKaptcha);
//生成验证码的文本
String createText = validateCodeUtil.createText();
//验证码文本的Session的保存
request.getSession().setAttribute(VerifyCode, createText);
//生成图片,并显示(html调用的显示)
validateCodeUtil.writeToPage(response);
}
//验证码验证是否通过
@RequestMapping("/validateDefaultKaptcha")
@ResponseBody
public boolean validateDefaultKaptcha(String tryCode,HttpServletRequest request,HttpServletResponse response) {
String Vcode = (String) request.getSession().getAttribute(VerifyCode);
if(Vcode!=null && Vcode.equals(tryCode)) {
return true;
}
return false;
}