1、在pom文件中先导入生成图像的依赖包
<dependency> <groupId>com.baomidou</groupId> <artifactId>kaptcha-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency>
2、在config写配置类
@Configuration public class CaptchaConfig { /** * 验证码配置 * Kaptcha配置类名 * * @return */ @Bean @Qualifier("captchaProducer") public DefaultKaptcha kaptcha() { DefaultKaptcha kaptcha = new DefaultKaptcha(); Properties properties = new Properties(); //验证码个数 properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4"); //字体间隔 properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE,"8"); //干扰线颜色 //干扰实现类 properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise"); //图片样式 properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.WaterRipple"); //文字来源 properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "0123456789"); Config config = new Config(properties); kaptcha.setConfig(config); return kaptcha; } }
3、配置好redis相关的配置
4、存入redis
- 根据请求获取客户ip作为key,方法如下:
-
** * 获取ip * @param request * @return */ public static String getIpAddr(HttpServletRequest request) { String ipAddress = null; try { ipAddress = request.getHeader("x-forwarded-for"); if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("Proxy-Client-IP"); } if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("WL-Proxy-Client-IP"); } if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getRemoteAddr(); if (ipAddress.equals("127.0.0.1")) { // 根据网卡取本机配置的IP InetAddress inet = null; try { inet = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } ipAddress = inet.getHostAddress(); } } // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length() // = 15 if (ipAddress.indexOf(",") > 0) { ipAddress = ipAddress.substring(0, ipAddress.indexOf(",")); } } } catch (Exception e) { ipAddress=""; } return ipAddress; }
5、controller类编写
@RestController @RequestMapping("/api/v1/captchar") public class CaptCharController { @Autowired private RedisTemplate redisTemplate; @Autowired private Producer captchaProducer; //captchaProducer和配置文件的@Qualifier("captchaProducer")名字相同才可以
@GetMapping("get_captcha") public void getCaptChar(HttpServletRequest request, HttpServletResponse response){ String producerText = captchaProducer.createText(); String key = getKey(request); //设置过期时间为10分钟 redisTemplate.opsForValue().set(key,producerText,10, TimeUnit.MINUTES); BufferedImage image = captchaProducer.createImage(producerText); ServletOutputStream outputStream = null; try { outputStream = response.getOutputStream(); ImageIO.write(image,"jpg",outputStream); outputStream.flush(); outputStream.close(); }catch (Exception e){ e.fillInStackTrace(); } }
//获取key的值 private String getKey(HttpServletRequest request){ String ipAddr = CommonUtil.getIpAddr(request); String header = request.getHeader("User-Agent"); String key = "user-service:captcha:"+CommonUtil.MD5(ipAddr+header); return key; } }