我们以前使用的令牌,修改密码后,旧的令牌仍然可以使用,相当于仍然可以使用旧密码“登录”这很危险。这时候需要使用redis让旧令牌主动失效。
实现思路:
借助redis,当用户登录成功之后,依然需要生成令牌,但这个令牌它在响应给浏览器的同时也需要往redis中存储一份。当浏览器携带着令牌去访问其他资源时,在拦截器这一块不仅要对浏览器携带的令牌进行合法性的校验,还需要从redis中获取一份一摸一样的令牌,如果能获取到就证明浏览器携带的令牌他并没有失效,获取不到则当作失效令牌处理,服务器不再提供服务。有了这个机制,当用户修改密码后,把redis中存储的这个一摸一样的令牌把它删除掉。
SpringBoot集成redis
1. 导入spring-boot-starter-data-redis起步依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2. 在yml配置文件中,配置redis连接信息(比如redis服务器的ip地址)
server:
port: 9090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/big_event
username: root
password: 123456
data:
redis:
host: localhost
port: 6379
mybatis:
configuration:
map-underscore-to-camel-case: true
3. 调用API(StringRedisTemplate)完成字符串的存取操作(在redis的起步依赖中,它会自动的往容器中注入一个stringRedisTemplate对象,如果要使用这个stringRedisTemplate不用手动new,直接从容器中获取就可以了
在test包下新建一个测试RedisTest.java类测试
package org.exampletest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.util.concurrent.TimeUnit;
@SpringBootTest//如果在测试类上添加了这个注解,那么将来单元测试方法执行之前,会先初始化Spring容器
public class RedisTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void testSet() {
//往redis中存储一个键值对 StringRedisTemplate
ValueOperations<String,String> ops= stringRedisTemplate.opsForValue();
ops.set("username","张三");
//存储键值对的时候给它一个过期的时间,这个是15s
ops.set("id","1",15, TimeUnit.SECONDS);
}
public void testGet() {
//从redis中获取一个键值对
ValueOperations<String,String> operations= stringRedisTemplate.opsForValue();
System.out.println(operations.get("username"));
}
}
令牌的主动失效
- 登录成功后,给浏览器响应令牌的同时,把该令牌存储到redis中
@PostMapping("/login")
public Result<String> login(@Pattern(regexp="^\\S{5,16}$") String username,@Pattern(regexp="^\\S{5,16}$") String password){
//根据用户名查询用户名
User loginUser = userService.findByUserName(username);
//判断用户是否存在
if(loginUser==null){
return Result.error("用户名不存在");
}
//判断密码是否正确 loginuser对象中密码是密文
if(Md5Util.getMD5String(password).equals(loginUser.getPassword())){
//登录成功
Map<String,Object>claims = new HashMap<>();
claims.put("id",loginUser.getId());
claims.put("username",loginUser.getUsername());
String token=JwtUtil.genToken(claims);
//把token存储在redis中
ValueOperations<String,String>operations = stringRedisTemplate.opsForValue();
operations.set(token,token,12, TimeUnit.HOURS);
return Result.success(token);
}
return Result.error("密码错误");
}
- LoginInterceptor拦截器中,需要验证浏览器携带的令牌,并同时需要获取到redis中存储的与之相同的令牌
package org.exampletest.interceptors;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.exampletest.pojo.Result;
import org.exampletest.utils.JwtUtil;
import org.exampletest.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import java.util.Map;
@Component
public class LoginInterceptor implements HandlerInterceptor {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
//令牌验证
String token=request.getHeader("Authorization");
//验证token
try{
//从redis中获取相同的token
ValueOperations<String,String>operations = stringRedisTemplate.opsForValue();
String redisToken= operations.get("token");
if(redisToken==null)
{
throw new RuntimeException();
}
Map<String, Object> claims= JwtUtil.parseToken(token);
//把业务数据存储到ThreadLocal中
ThreadLocalUtil.set(claims);
return true;
}catch (Exception e){
response.setStatus(401);
//不放行
return false;
}
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception{
//移除ThreadLocal中的数据
ThreadLocalUtil.remove();
}
}
- 当用户修改密码成功后,删除redis中存储的旧令牌
@PatchMapping("/updatePwd")
public Result updatePwd(@RequestBody Map<String,String> params,@RequestHeader("Authorization") String token){
//1.校验参数,没有提供相应的注解能满足,需要手动校验参数
String oldPwd=params.get("old_pwd");
String newPwd=params.get("new_pwd");
String rePwd =params.get("re_pwd");
if(!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)){
return Result.error("缺少必要的参数");
}
//原密码是否正确
//根据用户名查询用户拿到密码
Map<String,Object> map = ThreadLocalUtil.get();
String username = (String) map.get("username");
User loginUser = userService.findByUserName(username);
if(! loginUser.getPassword().equals(Md5Util.getMD5String(oldPwd))){
return Result.error("原密码不正确");
}
//校验newPwd与rePwd是否一致
if(!newPwd.equals(rePwd)){
return Result.error("两次输入的新密码不一致");
}
//2.调用service完成密码更新
Integer id = (Integer) map.get("id");
userService.updatePwd(newPwd,id);
//删除redis中对应的token
ValueOperations<String,String>operations = stringRedisTemplate.opsForValue();
operations.getOperations().delete(token);
return Result.success();
}
标签:令牌,String,import,redis,token,org,------,失效
From: https://blog.csdn.net/qq_74474809/article/details/140928642