服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|

服务器之家 - 编程语言 - JAVA教程 - 实例详解Spring Boot实战之Redis缓存登录验证码

实例详解Spring Boot实战之Redis缓存登录验证码

2020-12-17 14:38sun_t89 JAVA教程

本章简单介绍redis的配置及使用方法,本文示例代码在前面代码的基础上进行修改添加,实现了使用redis进行缓存验证码,以及校验验证码的过程。感兴趣的的朋友一起看看吧

本章简单介绍redis的配置及使用方法,本文示例代码在前面代码的基础上进行修改添加,实现了使用redis进行缓存验证码,以及校验验证码的过程。

1、添加依赖库(添加redis库,以及第三方的验证码库)

?
1
2
3
4
5
6
7
8
9
       <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的配置信息

?
1
2
3
4
5
6
7
8
9
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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插入操作关键代码

?
1
2
3
4
@autowired
  private redistemplate<string, string> redistemplate;
//将验证码以<key,value>形式缓存到redis
    redistemplate.opsforvalue().set(uuid, captcha.getanswer(), captchaexpires, timeunit.seconds);

完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@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值

实例详解Spring Boot实战之Redis缓存登录验证码

2)查看redis,可以查看到之前缓存的key value

实例详解Spring Boot实战之Redis缓存登录验证码

3)登录获取token时,添加验证码参数

如果验证码错误,返回验证码错误

实例详解Spring Boot实战之Redis缓存登录验证码

验证码正确,且用户名密码正确,返回token

实例详解Spring Boot实战之Redis缓存登录验证码

实例详解Spring Boot实战之Redis缓存登录验证码

总结

以上所述是小编给大家介绍的实例详解spring boot实战之redis缓存登录验证码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://blog.csdn.net/sun_t89/article/details/51944252

延伸 · 阅读

精彩推荐