|
|
@@ -53,6 +53,9 @@ public class ApiTokenService {
|
|
|
@Value("${api-token.refreshTime}")
|
|
|
private int refreshTime;
|
|
|
|
|
|
+ @Value("${api-token.redisExpireTime:691200}")
|
|
|
+ private int redisExpireTime;
|
|
|
+
|
|
|
@Autowired
|
|
|
private StringRedisTemplate stringRedisTemplate;
|
|
|
|
|
|
@@ -122,7 +125,7 @@ public class ApiTokenService {
|
|
|
|
|
|
// 缓存用户信息至redis
|
|
|
Long userId = (Long) claims.get(Constants.USER_ID);
|
|
|
- cacheUserInfo(userId, token);
|
|
|
+ cacheUserInfo(userId, token, expirationDate);
|
|
|
|
|
|
return token;
|
|
|
}
|
|
|
@@ -177,13 +180,13 @@ public class ApiTokenService {
|
|
|
return getClaimFromToken(token, Claims::getExpiration);
|
|
|
}
|
|
|
|
|
|
- private void cacheUserInfo(Long userId, String token) {
|
|
|
+ private void cacheUserInfo(Long userId, String token, Date expireTime) {
|
|
|
UserVo userVo = userService.getUserById(userId, true);
|
|
|
UserCacheInfo userCacheInfo = BeanCopyUtils.copy(userVo, UserCacheInfo.class);
|
|
|
-
|
|
|
if (userCacheInfo == null) {
|
|
|
throw new ServiceException("用户不存在");
|
|
|
}
|
|
|
+ userCacheInfo.setExpireTime(expireTime);
|
|
|
// 查询是否为管理老师
|
|
|
ManagementTeacherDto managementTeacherDto = managementTeacherApi.getByPhoneNumber(userVo.getMobile(), true);
|
|
|
if (managementTeacherDto != null) {
|
|
|
@@ -196,9 +199,12 @@ public class ApiTokenService {
|
|
|
}
|
|
|
|
|
|
// 缓存至redis
|
|
|
- stringRedisTemplate.opsForValue().set(Constants.JWT_TOKEN + token, JSONUtil.toJsonStr(userCacheInfo), expireTime, TimeUnit.SECONDS);
|
|
|
+ stringRedisTemplate.opsForValue().set(Constants.JWT_TOKEN + token, JSONUtil.toJsonStr(userCacheInfo), redisExpireTime, TimeUnit.SECONDS);
|
|
|
+
|
|
|
// 缓存用户在线用户userId,方便强制踢出
|
|
|
stringRedisTemplate.opsForSet().add(Constants.JWT_ONLINE_USER_TOKEN + userId, token);
|
|
|
+ // 每次登录,刷新该用户的在线token列表的缓存时间
|
|
|
+ stringRedisTemplate.expire(Constants.JWT_ONLINE_USER_TOKEN + userId, redisExpireTime, TimeUnit.SECONDS);
|
|
|
}
|
|
|
|
|
|
}
|