Spring Boot实战之Redis缓存登录验证码
来源:互联网 发布:淘宝热卖排行 编辑:程序博客网 时间:2024/06/05 08:40
Spring Boot实战之Redis缓存登录验证码
本章简单介绍Redis的配置及使用方法,本文示例代码在前面代码的基础上进行修改添加,实现了使用redis进行缓存验证码,以及校验验证码的过程。
1、添加依赖库(添加redis库,以及第三方的验证码库)
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId></dependency><dependency><groupId>cn.apiclub.tool</groupId><artifactId>simplecaptcha</artifactId><version>1.2.2</version></dependency>
spring.redis.database=4spring.redis.host=hostnamespring.redis.password=passwordspring.redis.port=6379spring.redis.timeout=2000spring.redis.pool.max-idle=8spring.redis.pool.min-idle=0spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1
3、添加redis数据模版
新增RedisConfig.java
package com.xiaofangtech.sun.config;import org.springframework.context.annotation.Bean;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.StringRedisSerializer;public class RedisConfig {@Bean JedisConnectionFactory jedisConnectionFactory() { return new JedisConnectionFactory(); }@Bean RedisTemplate<String, String>redisTemplate(RedisConnectionFactory factory){RedisTemplate<String, String> template = new RedisTemplate<String, String>();template.setConnectionFactory(jedisConnectionFactory());template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new StringRedisSerializer());return template;}}
4、redis的基本使用(缓存生成的验证码信息)
新建CaptchaModule.java,涉及redis插入操作关键代码
@Autowiredprivate RedisTemplate<String, String> redisTemplate;
//将验证码以<key,value>形式缓存到redisredisTemplate.opsForValue().set(uuid, captcha.getAnswer(), captchaExpires, TimeUnit.SECONDS);
完整代码
package com.xiaofangtech.sunt.utils;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.UUID;import java.util.concurrent.TimeUnit;import javax.imageio.ImageIO;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import cn.apiclub.captcha.Captcha;import cn.apiclub.captcha.backgrounds.GradiatedBackgroundProducer;import cn.apiclub.captcha.gimpy.FishEyeGimpyRenderer;@RestController@RequestMapping("captcha")public class CaptchaModule {@Autowiredprivate RedisTemplate<String, String> redisTemplate;private static int captchaExpires = 3*60; //超时时间3minprivate static int captchaW = 200;private static int captchaH = 60;@RequestMapping(value = "getcaptcha", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)public @ResponseBody byte[] getCaptcha(HttpServletResponse response){//生成验证码String uuid = UUID.randomUUID().toString();Captcha captcha = new Captcha.Builder(captchaW, captchaH) .addText().addBackground(new GradiatedBackgroundProducer()) .gimp(new FishEyeGimpyRenderer()) .build();//将验证码以<key,value>形式缓存到redisredisTemplate.opsForValue().set(uuid, captcha.getAnswer(), captchaExpires, TimeUnit.SECONDS);//将验证码key,及验证码的图片返回Cookie cookie = new Cookie("CaptchaCode",uuid); response.addCookie(cookie); ByteArrayOutputStream bao = new ByteArrayOutputStream(); try {ImageIO.write(captcha.getImage(), "png", bao);return bao.toByteArray();} catch (IOException e) {return null;}}}
5、redis内容的获取(根据key获取验证码)
完善前面获取token的流程,在获取token的接口中添加校验验证码的流程(根据登录参数中的验证码id获取验证码内容,并与登录参数中的验证码内容进行比对)
修改JsonWebToken.java
@Autowiredprivate RedisTemplate<String, String> redisTemplate;
//验证码校验在后面章节添加String captchaCode = loginPara.getCaptchaCode();try {if (captchaCode == null){throw new Exception();}String captchaValue = redisTemplate.opsForValue().get(captchaCode);if (captchaValue == null){throw new Exception();}redisTemplate.delete(captchaCode);if (captchaValue.compareTo(loginPara.getCaptchaValue()) != 0){throw new Exception();}} catch (Exception e) {resultMsg = new ResultMsg(ResultStatusCode.INVALID_CAPTCHA.getErrcode(), ResultStatusCode.INVALID_CAPTCHA.getErrmsg(), null);return resultMsg;}
6、测试
1)请求获取验证码,可以获取到验证码图片,以及在cookie中返回缓存入redis的key值
2)查看redis,可以查看到之前缓存的key value
3)登录获取token时,添加验证码参数
如果验证码错误,返回验证码错误
验证码正确,且用户名密码正确,返回token
2 0
- Spring Boot实战之Redis缓存登录验证码
- Spring Boot缓存实战 Redis
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
- spring boot之缓存Redis
- Spring Boot缓存实战 Redis + Caffeine 实现多级缓存
- Spring Boot 拦截器之验证登录
- Spring Boot缓存实战 EhCache
- Spring Boot缓存实战 Caffeine
- Spring Boot Redis 数据缓存
- spring boot redis注解缓存。
- Spring boot 使用 Redis 缓存
- spring boot redis缓存JedisPool
- spring boot项目实战:redis
- Spring Boot缓存实战 Redis 设置有效时间和自动刷新缓存-2
- spring boot学习4之mybatis+redis缓存整合
- spring boot之redis
- spring boot 之 redis
- spring Boot 生成图片验证码 存入redis
- app 欢迎界面
- Snackbar提示框
- Google FlatBuffers使用教程
- Spring Session入门(包含示例项目)
- Linux网络配置
- Spring Boot实战之Redis缓存登录验证码
- 面经总结
- 在Android原生中嵌入React Native,进而React Native调用原生
- 删除主数据的步骤
- php图像处理大全
- Codeforces 208A Dubstep
- WPF向指定窗口发送键盘指令
- linux笔记
- Android ImageView加载圆形图片且同时绘制圆形图片的外部边缘边线及边框