码迷,mamicode.com
首页 > 其他好文 > 详细

redis获取图形验证码

时间:2021-05-24 06:35:16      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:remote   ase   Servle   过期   dex   获取   ati   obs   trace   

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

  1. 根据请求获取客户ip作为key,方法如下:
  2. **
         * 获取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; } }

redis获取图形验证码

标签:remote   ase   Servle   过期   dex   获取   ati   obs   trace   

原文地址:https://www.cnblogs.com/laolehyf/p/14759010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!