SpringBoot+Redis集成验证码的简单案例

2021-08-21  本文已影响0人  今年的草莓味奶茶超级好喝

1、引入redis依赖

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-pool2</artifactId>
       <version>2.10.0</version>
</dependency>

验证码依赖

<dependency>
       <groupId>com.github.whvcse</groupId>
       <artifactId>easy-captcha</artifactId>
       <version>1.6.2</version>
</dependency>

2、编写RedisConfig

@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        StringRedisSerializer keySerializer = new StringRedisSerializer();
        GenericFastJsonRedisSerializer valueSerializer = new GenericFastJsonRedisSerializer();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 序列化 key 和 value
        redisTemplate.setKeySerializer(keySerializer);
        redisTemplate.setValueSerializer(valueSerializer);
        redisTemplate.setHashKeySerializer(keySerializer);
        redisTemplate.setHashValueSerializer(valueSerializer);
        return redisTemplate;
    }
}

3、编写RedisUtils

@Component
public class RedisClient {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    /*
     * @Desc TODO   设置缓存(没有时间限制)
     * @Date 2021-07-24 16:11
     * @Version 1.0
     */
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }
    /*
     * @Desc TODO   设置缓存(有时间限制,单位为 秒)
     * @Date 2021-07-24 16:11
     * @Version 1.0
     */
    public void set(String key, Object value, long timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }
    /*
     * @Desc TODO   删除缓存,并返回是否删除成功
     * @Date 2021-07-24 16:11
     * @Version 1.0
     */
    public boolean delete(String key) {
        redisTemplate.delete(key);
        // 如果还存在这个 key 就证明删除失败
        if (redisTemplate.hasKey(key)) {
            return false;
            // 不存在就证明删除成功
        } else {
            return true;
        }
    }
    /*
     * @Desc TODO   取出缓存
     * @Date 2021-07-24 16:12
     * @Version 1.0
     */
    public Object get(String key) {
        if (redisTemplate.hasKey(key)) {
            return redisTemplate.opsForValue().get(key);
        } else {
            return null;
        }
    }
    /*
     * @Desc TODO   获取失效时间(-2:失效 / -1:没有时间限制)
     * @Date 2021-07-24 16:15
     * @Version 1.0
     */
    public long getExpire(String key) {
        // 判断是否存在
        if (redisTemplate.hasKey(key)) {
            return redisTemplate.getExpire(key);
        } else {
            return Long.parseLong(-2 + "");
        }
    }

4、编写controller测试

@Controller
@ResponseBody
public class CaptchaController {
    @Resource
    private RedisClient redisClient;
    @GetMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        specCaptcha.setFont(Captcha.FONT_1);
        String id = UUID.randomUUID().toString();
        response.setHeader("id", id);
        CaptchaUtil.out(specCaptcha, request, response);
        String verCode = specCaptcha.text().toLowerCase();
        redisClient.set(id, verCode);//可设置有效时间,单位为秒
    }
    @PostMapping(value = "/check")
    public boolean check(@RequestBody String info) {
        JSONObject jsonObject = JSON.parseObject(info);
        String id = jsonObject.getString("id");
        String code = jsonObject.getString("code");
        String s = redisClient.get(id).toString();//id
        return code.equalsIgnoreCase(s);
    }
}
上一篇下一篇

猜你喜欢

热点阅读