在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
1 package com.xiaostudy.shiro_test1.config; 2 3 import com.google.code.kaptcha.impl.DefaultKaptcha; 4 import com.google.code.kaptcha.util.Config; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 8 import java.util.Properties; 9 10 /** 11 * Created with IntelliJ IDEA. 12 * User: Administrator 13 * Date: 2019/7/17 14 * Time: 23:46 15 * Description: No Description 16 */ 17 @Configuration 18 public class KaptchaConfig { 19 20 @Bean 21 public DefaultKaptcha getDefaultKaptcha(){ 22 DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); 23 Properties properties = new Properties(); 24 // 图片边框,合法值:yes[默认] , no 25 properties.setProperty("kaptcha.border", "yes"); 26 // 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.默认black 27 properties.setProperty("kaptcha.border.color", "105,179,90"); 28 // 边框厚度,合法值:>0,默认1 29 // properties.setProperty("kaptcha.border.thickness", "1"); 30 // 图片宽,默认200 31 properties.setProperty("kaptcha.image.width", "110"); 32 // 图片高,默认50 33 properties.setProperty("kaptcha.image.height", "40"); 34 // 字体大小,默认40px 35 properties.setProperty("kaptcha.textproducer.font.size", "30"); 36 // 字体,默认Arial, Courier 37 properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑"); 38 // 字体颜色,合法值: r,g,b 或者 white,black,blue.默认black 39 properties.setProperty("kaptcha.textproducer.font.color", "blue"); 40 // session key,默认KAPTCHA_SESSION_KEY 41 properties.setProperty("kaptcha.session.key", "code"); 42 // session date,默认KAPTCHA_SESSION_DATE 43 // properties.setProperty("kaptcha.session.date", "KAPTCHA_SESSION_DATE"); 44 // 验证码长度,默认5 45 properties.setProperty("kaptcha.textproducer.char.length", "4"); 46 // 文字间隔,默认2 47 // properties.setProperty("kaptcha.textproducer.char.space", "2"); 48 // 干扰 颜色,合法值: r,g,b 或者 white,black,blue.默认black 49 // properties.setProperty("kaptcha.noise.color", "black"); 50 // 更多可参考:https://blog.csdn.net/elephantboy/article/details/52795309 51 52 Config config = new Config(properties); 53 defaultKaptcha.setConfig(config); 54 55 return defaultKaptcha; 56 } 57 }
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"); // 下面就是匹配这两个是否相同
原文地址:https://www.cnblogs.com/xiaostudy/p/11225187.html
时间: 2024-10-09 01:12:39