1.pom.xml引入kaptcha所需要的jar包

     <!-- 验证码 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>

2.添加KaptchaConfig类

package com.demo.common;

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component; import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.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;
}
}

3.添加获取验证码的COntroller

package com.demo.controller;

import java.awt.image.BufferedImage;
import java.io.IOException; import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer; @Controller
@RequestMapping(value = "/captcha")
public class CaptchaController { private Producer captchaProducer = null; @Autowired
public void setCaptchaProducer(Producer captchaProducer) {
this.captchaProducer = captchaProducer;
} /**
*
* 获取验证码图片
*
* @param request
* @param response
* @return
* @throws IOException
*/
@RequestMapping("getCaptchaCode")
public ModelAndView getCaptchaCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession(); 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"); // 生成验证码文本
String capText = captchaProducer.createText();
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
// 利用生成的字符串构建图片
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out); try {
out.flush();
} finally {
out.close();
}
return null;
}
}

4.前台配置(form表单中添加验证码,我使用的是thymeleaf模板引擎,语法可参考官网https://www.thymeleaf.org/

          <form class="registerform" id="form1">
<div class="fm-item">
<label for="logonId" class="form-label">登陆账号:</label>
<input type="text" maxlength="100" id="username" name="username" class="i-text"/>
<div class="ui-form-explain"></div>
</div>
<div class="fm-item">
<label for="logonId" class="form-label">登陆密码:</label>
<input type="password" maxlength="100" id="password" name="password" class="i-text"/>
<div class="ui-form-explain"></div>
</div>
<div class="fm-item pos-r">
<label for="logonId" class="form-label">验证码</label>
<input type="text" maxlength="100" id="yzm" name="yzm" class="i-text yzm"/>
<div class="ui-form-explain">
<img id="captchaImg" alt="" th:src="@{/captcha/getCaptchaCode}" class="yzm-img" style="width: 100px;height: 40px;line-height:40px;"/>
</div>
</div>
<div class="fm-item">
<label for="logonId" class="form-label"></label>
<input type="button" value="" tabindex="4" id="submit" class="btn-login"/>
<div class="ui-form-explain"></div>
</div>
</form>

  

 

5.js部分 点击验证码获取新的验证码

//获取新验证码
$('#captchaImg').click(function() {
$(this).attr('src', '/captcha/getCaptchaCode.jpg');
});

最新文章

  1. [个人翻译]Redis 集群教程(上)
  2. DeepMind背后的人工智能:深度学习原理初探
  3. 采用阿里的API进行动态域名解析
  4. the king of fighter
  5. 前端学习 第五弹: CSS (一)
  6. Hive安装
  7. Linux中如何解压iso类型文件
  8. C# winform解决解决窗体第一次设置为最大化后,点击最大化按钮窗体无法居中问题
  9. Python设计模式——单例模式
  10. WIN8 WIN10系统如何完全获取用户管理员权限
  11. Gstreamer的一些基本概念与A/V同步分析
  12. curl几个选项
  13. /etc/security/limits.conf 设置
  14. caffe+opencv3.3dnn模块 完成手写数字图片识别
  15. JavaScript es2015经验基础总结
  16. MySQL用户管理、常用sql语句、MySQL数据库备份恢复
  17. Redis源代码剖析--对象object
  18. iOS UI基础-1.0加法计算器
  19. MySQL之表连接(内外连接和重命名的使用)
  20. 手写简易WEB服务器

热门文章

  1. 【Oracle/Maven】Maven导入oracle11g 自携带jdbc驱动包ojdbc6.jar到本地库
  2. C#中 Dictionary&lt;&gt;的使用及注意事项
  3. Bitmap: 使用Bitmap作为绘图缓冲时设置抗锯齿
  4. 常用API的注意事项
  5. iOS 点击空白处收回键盘的几个简单代码
  6. java通过telnet远程至windows机器执行dos命令
  7. python调用HTMLTestRunner+unittest实现一次执行多个测试类,并生成与每个测试类对应的测试报告,具体看代码,附上整个project代码
  8. Node.js中使用pipe拷贝大文件不能完全拷贝的解决办法
  9. wordpress通过$wpdp更新数据表内容
  10. DDS工作原理及其性能分析