您好,欢迎访问一九零五行业门户网

SpringBoot怎么整合Redis缓存验证码

1、简介redis is an open source (bsd licensed), in-memory data structure store, used as a database, cache, and message broker.
翻译:redis 是一个开源的内存中的数据结构存储系统,它可以用作:数据库、缓存和消息中间件。
redis 是用 c 语言开发的一个开源的高性能键值对(key-value)数据库,官方提供的数据是可以达到 **100000+**的 qps。
qps(queries-per-second),每秒内查询次数。(百度百科)
它存储的 value 类型比较丰富,也被称为结构化的 nosql 数据库。
nosql(not only sql),不仅仅是 sql,泛指非关系型数据库。
nosql 数据库并不是要取代关系型数据库,而是关系型数据库的补充。
关系型数据库(rdbms)
mysql
oracle
db2
sql server
非关系型数据库(nosql)
redis
mongo db
memcached
redis 应用场景
缓存
任务队列
消息队列
分布式锁
2、介绍reddis官方推荐的java客户端有三种:jedis、lettuce、redisson。
spring 对 redis 客户端进行了整合,提供了 spring data redis。
在 spring boot 项目中还提供了对应的 starter,即 spring-boot-starter-data-redis。
在这里直接使用的是spring data redis,且不展示redis的下载和安装过程啦。
3、前期配置3.1、坐标导入在创建完成spring boot项目之后,在pom.xml中加入spring-boot-start-data-redis的依赖坐标
<!--spring data redis--><dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid></dependency>
3.2、配置文件这里忽略了其他的配置如mysql这些,只突出redis的配置信息
spring:
# redis配置
redis:
host: localhost
port: 6379
# 根据自己设置的密码决定
password: 123456
# 操作0号数据库,默认有16个数据库
database: 0
jedis:
pool:
max-active: 8 # 最大连接数
max-wait: 1ms # 连接池最大阻塞等待时间
max-idle: 4 # 连接池中的最大空闲连接
min-idle: 0 # 连接池中的最小空闲连接
3.3、配置类使用专门的客户端接口操作,将 redis 整合到 spring boot 中。这里采用的是 redistemplate 工具类,该工具类由 springboot 框架提供。
redistemplate 在处理 key 和 value 时,会对其进行序列化操作,这就会导致一些问题。
比如:输入的 key 值是 city,但 redis 获得到的 key 值却是 \xac]xed\x00\x05t\x00\x04city。
因此就需要一个专门的配置类,来专门处理 redistemplate 默认的序列化处理所导致的问题。
值得注意的是,这里是重新加载一个新的序列化器来取代原来的序列化器,这就证明着其原本是有着自己默认的序列化器jdkserializationredisserializer。
/** * @classname redisconfig * @description redis配置类,更换key的默认序列化器 * @author xbaozi * @date 19:04 2022/7/2 **/@configurationpublic class redisconfig extends cachingconfigurersupport { @bean public redistemplate<object, object> redistemplate(redisconnectionfactory connectionfactory) { redistemplate<object, object> redistemplate = new redistemplate<>(); redistemplate.setkeyserializer(new stringredisserializer()); redistemplate.sethashkeyserializer(new stringredisserializer()); redistemplate.setconnectionfactory(connectionfactory); return redistemplate; }}
4、java操作redis在这里由于代码比较隐私(主要是懒得整理登录和生成验证码的接口放在这里),因此这里就直接使用测试类进行演示。
发送验证码
@postmapping("/sendmsg")public result<string> sendmsg(@requestbody user user, httpsession session) { // 获取需要发送短信的手机号 string userphone = user.getphone(); if (stringutils.isnotempty(userphone)) { // 随机生成4位验证码 string checkcode = validatecodeutils.generatevalidatecode4string(4); // 将生成的验证码保存到redis中并设置有效期五分钟 redistemplate.opsforvalue().set(userphone, checkcode, 5, timeunit.minutes); return result.success(checkcode); } return result.error("短信发送错误");}
输入验证码登录
@postmapping("/login")public result<user> login(@requestbody map map, httpsession session) { log.info("map: {}", map); // 获取用户输入信息 string phone = (string)map.get("phone"); string code = (string)map.get("code"); // 从redis中取出验证码 string checkcode = redistemplate.opsforvalue().get(phone); // 比对验证码是否一致 if (stringutils.isnotempty(checkcode) && checkcode.equals(code.tolowercase())) { // 将用户id存放到session中 session.setattribute("user", user.getid()); // 登录成功,删除redis中的验证码 redistemplate.delete(phone); // 将用户信息返回到前端 return result.success(user); } return result.error("登录失败,请检查手机号或验证码是否正确");}
以上就是springboot怎么整合redis缓存验证码的详细内容。
其它类似信息

推荐信息