实例详解Spring Boot实战之Redis缓存登录验证码
程序员文章站
2024-02-23 13:15:46
本章简单介绍redis的配置及使用方法,本文示例代码在前面代码的基础上进行修改添加,实现了使用redis进行缓存验证码,以及校验验证码的过程。
1、添加依赖库(添加red...
本章简单介绍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>
2、在application.properties中添加redis的配置信息
spring.redis.database=4 spring.redis.host=hostname spring.redis.password=password spring.redis.port=6379 spring.redis.timeout=2000 spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.redis.pool.max-active=8 spring.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插入操作关键代码
@autowired private redistemplate<string, string> redistemplate; //将验证码以<key,value>形式缓存到redis redistemplate.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 { @autowired private redistemplate<string, string> redistemplate; private static int captchaexpires = 3*60; //超时时间3min private 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>形式缓存到redis redistemplate.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
@autowired private 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
总结
以上所述是小编给大家介绍的实例详解spring boot实战之redis缓存登录验证码,希望对大家有所帮助