在pom.xml引入依赖

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

配置类KaptchaConfig.java

 package com.xiaostudy.shiro_test1.config;

 import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import java.util.Properties; /**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 2019/7/17
* Time: 23:46
* Description: No Description
*/
@Configuration
public class KaptchaConfig { @Bean
public DefaultKaptcha getDefaultKaptcha(){
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
// 图片边框,合法值:yes[默认] , no
properties.setProperty("kaptcha.border", "yes");
// 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.默认black
properties.setProperty("kaptcha.border.color", "105,179,90");
// 边框厚度,合法值:>0,默认1
// properties.setProperty("kaptcha.border.thickness", "1");
// 图片宽,默认200
properties.setProperty("kaptcha.image.width", "110");
// 图片高,默认50
properties.setProperty("kaptcha.image.height", "40");
// 字体大小,默认40px
properties.setProperty("kaptcha.textproducer.font.size", "30");
// 字体,默认Arial, Courier
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
// 字体颜色,合法值: r,g,b 或者 white,black,blue.默认black
properties.setProperty("kaptcha.textproducer.font.color", "blue");
// session key,默认KAPTCHA_SESSION_KEY
properties.setProperty("kaptcha.session.key", "code");
// session date,默认KAPTCHA_SESSION_DATE
// properties.setProperty("kaptcha.session.date", "KAPTCHA_SESSION_DATE");
// 验证码长度,默认5
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 文字间隔,默认2
// properties.setProperty("kaptcha.textproducer.char.space", "2");
// 干扰 颜色,合法值: r,g,b 或者 white,black,blue.默认black
// properties.setProperty("kaptcha.noise.color", "black");
// 更多可参考:https://blog.csdn.net/elephantboy/article/details/52795309 Config config = new Config(properties);
defaultKaptcha.setConfig(config); return defaultKaptcha;
}
}

html使用

<img alt="验证码" onclick = "this.src='/defaultKaptcha?d='+new Date().getTime()" src="/defaultKaptcha" />

验证码请求

/**
* 验证码请求
* @param response
* @param session
* @throws Exception
*/
@RequestMapping("/defaultKaptcha")
public void defaultKaptcha(HttpServletResponse response,HttpSession session) {
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
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
BufferedImage bi = captchaProducer.createImage(capText);
try {
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

登录认证验证码

// 从session中拿到正确的验证码
String captchaId = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
// 用户输入的验证码
String parameter = request.getParameter("vrifyCode");
// 下面就是匹配这两个是否相同

最新文章

  1. Fedora javac 命令提示 [javac: 未找到命令...]
  2. C# WebService输出JSON 实现二
  3. Ext GridPanel
  4. !! UML十四图打油诗记忆法
  5. JAXB - Annotations, The Object Factory: XmlRegistry, XmlElementDecl
  6. wifidog编译到openwrt
  7. 【HTML】Advanced5:Accessible Forms
  8. Aircrack-ng官方文档翻译[中英对照]---Airdecap-ng
  9. [转载]SharePoint 网站管理-PowerShell
  10. linux下查找文件和文件内容
  11. for循环与foreach
  12. Reactjs vs. Vuejs
  13. protobuf 编码实现解析(java)
  14. SpringMVC格式转化错误之HTTP Status [400] – [Bad Request]
  15. 【Codeforces 1137A】Skyscrapers
  16. 洛谷p1072 gcd,质因数分解
  17. ix 混合索引
  18. MVC5使用EF6 Code First--创建EF数据模型(一)
  19. jquery开发的数字相加游戏(你能玩几分)
  20. 20145204 《Java程序设计》第四周学习总结

热门文章

  1. 一个 Object.assign 的误解
  2. jmeter之timer --笔记一
  3. Chapter Two
  4. Oracle语法 及 SQL题目(一)
  5. JVM相关文章和GC原理算法
  6. JSON HiJacking攻击
  7. [电脑]拆解DELL 2007FPb液晶显示器
  8. Windowless
  9. HTMLPage测试js通过ajax调用
  10. HmacSHA256摘要算法2 MACCoder