登录token存入redis
This commit is contained in:
@@ -132,6 +132,9 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Resource
|
||||
private com.ai.da.common.security.config.SecurityProperties securityProperties;
|
||||
|
||||
@Resource
|
||||
private UserFollowService userFollowService;
|
||||
|
||||
@@ -344,7 +347,11 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
principal.setLanguage(account.getLanguage());
|
||||
principal.setCountry(account.getCountry());
|
||||
String token2 = jwtTokenHelper.createToken(principal);
|
||||
// 本地 JVM 缓存(适配旧逻辑)
|
||||
LocalCacheUtils.setTokenCache(String.valueOf(account.getId()), token2);
|
||||
// 同步写入 Redis,重启后仍然可用
|
||||
long jwtExpiration = securityProperties.getJwtExpiration();
|
||||
redisUtil.setLoginToken(account.getId(), token2, jwtExpiration);
|
||||
return token2;
|
||||
}
|
||||
|
||||
@@ -600,21 +607,25 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
|
||||
@Override
|
||||
public Boolean logout(AccountLogoutDTO accountLogoutDTO) {
|
||||
//jwt本身失效比较难做 统一用缓存实现 删除缓存就失效
|
||||
String token = LocalCacheUtils.getTokenCache(String.valueOf(accountLogoutDTO.getUserId()));
|
||||
if (StringUtils.isNotBlank(token)) {
|
||||
LocalCacheUtils.delTokenCache(String.valueOf(accountLogoutDTO.getUserId()));
|
||||
}
|
||||
// jwt 本身失效比较难做,统一用缓存实现:删除缓存即失效
|
||||
String userIdStr = String.valueOf(accountLogoutDTO.getUserId());
|
||||
LocalCacheUtils.delTokenCache(userIdStr);
|
||||
// 同时删除 Redis 中的 token,防止服务重启后仍然有效
|
||||
redisUtil.deleteLoginToken(accountLogoutDTO.getUserId());
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean isLogin(AccountLogoutDTO accountLogoutDTO) {
|
||||
String token = LocalCacheUtils.getTokenCache(String.valueOf(accountLogoutDTO.getUserId()));
|
||||
String userIdStr = String.valueOf(accountLogoutDTO.getUserId());
|
||||
// 先查本地缓存
|
||||
String token = LocalCacheUtils.getTokenCache(userIdStr);
|
||||
if (StringUtils.isNotBlank(token)) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
return Boolean.FALSE;
|
||||
// 本地没有时,再查 Redis,保证服务重启后也能判断登录状态
|
||||
String redisToken = redisUtil.getLoginToken(accountLogoutDTO.getUserId());
|
||||
return StringUtils.isNotBlank(redisToken);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -812,6 +823,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
if (StringUtils.isNotBlank(token)) {
|
||||
LocalCacheUtils.delTokenCache(String.valueOf(accountDelete.getId()));
|
||||
}
|
||||
// 删除 Redis 中对应的登录 token
|
||||
redisUtil.deleteLoginToken(accountDelete.getId());
|
||||
if (!userName.equals(userToBeUpdate.getUserName())) {
|
||||
// accountMapper.deleteById(accountDelete);
|
||||
log.info("排查用户被删除原因:deleteNoLoginRequired,true, 删除用户(改为降为游客)");
|
||||
@@ -1065,6 +1078,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
if (StringUtils.isNotBlank(token)) {
|
||||
LocalCacheUtils.delTokenCache(String.valueOf(account.getId()));
|
||||
}
|
||||
// 删除 Redis 中对应的登录 token
|
||||
redisUtil.deleteLoginToken(account.getId());
|
||||
// accountMapper.deleteById(account.getId());
|
||||
log.info("排查用户被删除原因:deleteNoLoginRequiredNew,删除用户(改为降为游客)");
|
||||
accountMapper.toVisitor(account.getId());
|
||||
|
||||
Reference in New Issue
Block a user