redisUtils.setKeyAndSetExpireTime(BusinessConstant.JWT_REDIS_KEY.getKey() + jwtTokenWithExpireTimeMinutes, user.getName() + ":" + user.getUserId(), expiredTime, TimeUnit.MINUTES);
时间: 2024-02-14 17:08:14 浏览: 22
这段代码是用来将用户的JWT Token和用户信息存储到Redis中,并且设置过期时间。其中,setKeyAndSetExpireTime是自定义的Redis工具类方法,包含以下参数:
- key:JWT Token在Redis中存储的key。
- value:用户信息,通过拼接用户名称和用户ID得到。
- expiredTime:Token的过期时间。
- TimeUnit.MINUTES:过期时间的单位,这里设置为分钟。
与前一个问题的代码相比,这段代码只存储了Token和用户信息,没有存储用于存储Token的key。因此,如果需要删除Token,需要直接使用key来删除。
相关问题
redisUtils.setNewAndDeleteOldWithExpiredTime(BusinessConstant.JWT_REDIS_KEY.getKey() + jwtTokenWithExpireTimeMinutes, user.getName() + ":" + user.getUserId(),BusinessConstant.JWT_REDIS_KEY.getKey() +authorizationToken, expiredTime, TimeUnit.MINUTES);
这段代码是用来将用户的JWT Token和用户信息绑定存储到Redis中,并且设置过期时间。其中,setNewAndDeleteOldWithExpiredTime是自定义的Redis工具类方法,包含以下参数:
- key1:JWT Token在Redis中存储的key,该key会随着Token的过期时间一同被删除。
- value1:用户信息,通过拼接用户名称和用户ID得到。
- key2:用于存储Token的key,该key不会随着Token的过期时间被删除。
- expiredTime:Token的过期时间。
- TimeUnit.MINUTES:过期时间的单位,这里设置为分钟。
else if (role.equals(UserType.TEACHER_USER.getCode())) { //若不是管理员,检查是不是老师 TeacherEntity teacherRes = loginService.teacherLogin(username, password); Optional.ofNullable(teacherRes).orElseThrow(() -> { return new BusinessCodeException(BusinessCode.USER_LOGIN_FAILURE.getMsg(), BusinessCode.USER_LOGIN_FAILURE.getCode()); }); UserVo userVo = new UserVo(); //封装页面用户实体 UserVo user = userVo.setUserId(teacherRes.getTeacherId()).setName(teacherRes.getTeacherName()).setRole(UserType.TEACHER_USER.getCode()); //生成jwt //过期时间 int expiredTime = Integer.parseInt(BusinessConstant.JWT_EXPIRED_TIME.getKey()); //分钟过期 String jwtTokenWithExpireTimeMinutes = JwtUtils.createJwtTokenWithExpireTimeMinutes(user, rsaProperties.getPrivateKey(), expiredTime, BusinessConstant.SYSTEM_JWT_PAYLOAD_KEY.getKey(), BusinessConstant.SYSTEM_JWT_ISS.getKey()); //设置redis过期时间 --测试(分钟) redisUtils.setKeyAndSetExpireTime(BusinessConstant.JWT_REDIS_KEY.getKey() + jwtTokenWithExpireTimeMinutes, user.getName() + ":" + user.getUserId(), expiredTime, TimeUnit.MINUTES); //返回token response.setHeader(BusinessConstant.JWT_REQUEST_HEAD.getKey(),jwtTokenWithExpireTimeMinutes); //封装返回类型 return ApiResultHandler.successResult(user); }
这段代码是用于用户登录验证的,如果登录的用户类型是老师,则调用 `loginService.teacherLogin(username, password)` 方法进行验证,如果验证成功,则将用户信息封装成一个 `UserVo` 对象,并生成一个 JWT token,然后将 token 放在响应头中返回。同时,将 token 存储到 Redis 中以便后续验证用户身份。最后,返回一个成功的响应结果,其中包含了用户信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)