Redis实现登录的优化

本文介绍了如何在SpringBoot应用中使用Redis来增强JWT令牌的登录验证,包括安装Redis、配置依赖、StringRedisTemplate的使用以及主动失效机制(如登录时存储令牌、修改密码时删除旧令牌和拦截器中的检验)。
摘要由CSDN通过智能技术生成

目录

1 前言

2 实现步骤

2.1 软件环境准备

2.1.1 Redis的安装

2.1.2 在pom.xml中添加依赖

2.1.3 在application.yml中进行相关配置

2.2 StringRedisTemplate的常用方法

2.2.1 获取operations

2.2.2 主要方法 

2.3 令牌主动失效机制

2.3.1 登录时将令牌存入Redis

2.3.2 修改密码时删除旧的令牌

2.3.3 拦截器中进行检验


1 前言

在我们使用JWT令牌进行登录验证的时候,不可避免的也会出现一些问题。比如:当用户修改密码后,旧的令牌仍然生效。因此,我们可以采用Redis进行登录的优化。

2 实现步骤

2.1 软件环境准备

2.1.1 Redis的安装

百度网盘的链接如下,解压即可使用

链接:https://pan.baidu.com/s/1nAXT6alX-pMxxSfAe6-tsw?pwd=0529 

提取码:0529

2.1.2 在pom.xml中添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

在添加依赖后,会自动向IOC容器中注入StringRedisTemplate对象,通过它我们可以对Redis数据库进行相关操作。

2.1.3 在application.yml中进行相关配置

#redis初始状态没密码,所以暂时不需要配置
spring:
  data:
    redis:
      port: 6379 #端口号
      host: localhost #本地
      database: 0 #redis有16个小库,默认0

2.2 StringRedisTemplate的常用方法

2.2.1 获取operations

@Autowired
private StringRedisTemplate stringRedisTemplate;
//其它
ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();

2.2.2 主要方法 

相关方法作用参数
operations.set("xx", "xx", 1, TimeUnit.HOURS);//过期时间为1小时向Redis中插入键值均为xx的数据

1.键(String)

2.值(String)

3.过期时间数(long)

4.时间数单位(TimeUnit.xx)

注:3和4可以不要

operations.get("xx");从Redis中获取键为xx的值键(String)
operations.getOperations().delete("xx");删除Rdis键为xx的键值对键(String)

2.3 令牌主动失效机制

2.3.1 登录时将令牌存入Redis

public class UserController {
    @PostMapping("/login")
    public Result<String> login(//其它) {
        //其它代码
        //获得JWT令牌
        String token = JwtUtil.genToken(claims);
        //将JWT令牌存入Redis
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        //过期时间要和JWT令牌保持一致
        operations.set(token, token, 1, TimeUnit.HOURS);
    }
    //其它代码
}

2.3.2 修改密码时删除旧的令牌

public class UserController {
    //其它代码
    @PatchMapping("/updatePwd")
    //@RequestHeader(name = "Authorization") String token从请求头中获取JWT令牌
    public Result updatePwd(@RequestBody Map<String, String> params, @RequestHeader(name = "Authorization") String token) {
        //其它代码
        //删除Redis中旧的JWT令牌
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.getOperations().delete(token);
    }
}

2.3.3 拦截器中进行检验

public class LoginInterceptor implements HandlerInterceptor {
    //其它
    @Override
    public boolean preHandle(//其它) {
        try {
            //其它代码
            //没获取到值则不放行
            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
            if (operations.get(token) == null) {
                throw new RuntimeException();
            }
            //放行
            return true;
        } catch (Exception e) {
            //不放行
            return false;
        }
    }
}

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厂里英才

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值