fix : 路由关键key改为登录uuid

This commit is contained in:
clay
2024-03-07 15:21:26 +08:00
parent c86f078b3d
commit d3d4b231cf
4 changed files with 13 additions and 6 deletions

View File

@@ -72,6 +72,7 @@ public class OnlineUserServiceImpl implements OnlineUserService {
@Override @Override
public void force(String tokenId) { public void force(String tokenId) {
redisTemplate.delete(CacheConstants.LOGIN_TOKEN_KEY + tokenId); redisTemplate.delete(CacheConstants.LOGIN_TOKEN_KEY + tokenId);
redisTemplate.delete(CacheConstants.ROUTE_CACHE_KEY + tokenId);
} }
private OnlineUser toOnlineUser(LoginUser user) { private OnlineUser toOnlineUser(LoginUser user) {

View File

@@ -307,6 +307,7 @@ public class UserServiceImpl implements UserService {
batchUserPost(dto, Boolean.TRUE); batchUserPost(dto, Boolean.TRUE);
dto.setPassword(null); dto.setPassword(null);
UserBase user = dto.toUser(); UserBase user = dto.toUser();
return userMapper.update(user); return userMapper.update(user);
} }

View File

@@ -31,6 +31,7 @@ import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList; import java.util.ArrayList;
@@ -95,7 +96,7 @@ public class LoginServiceImpl implements LoginService {
throw new CustomException("验证码错误!"); throw new CustomException("验证码错误!");
} }
redisTemplate.delete(uuid); redisTemplate.delete(uuid);
//用户验证 //用户验证
Authentication authentication = null; Authentication authentication = null;
try { try {
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
@@ -156,11 +157,15 @@ public class LoginServiceImpl implements LoginService {
@Override @Override
public List<RouterVo> getMenuRouterByUserId() { public List<RouterVo> getMenuRouterByUserId() {
List<RouterVo> result = (List<RouterVo>) redisTemplate.opsForValue().get(CacheConstants.ROUTE_CACHE_KEY + SecurityUtils.getUserId()); LoginUser loginUser = SecurityUtils.getLoginUser();
if (ObjectUtils.isEmpty(loginUser)){
throw new CustomException("获取用户信息失败!");
}
List<RouterVo> result = (List<RouterVo>) redisTemplate.opsForValue().get(CacheConstants.ROUTE_CACHE_KEY + loginUser.getUuid());
if (result == null || result.isEmpty()) { if (result == null || result.isEmpty()) {
RLock lock = redissonClient.getLock(CacheConstants.ROUTE_CACHE_KEY + "lock:" + SecurityUtils.getUserId()); RLock lock = redissonClient.getLock(CacheConstants.ROUTE_CACHE_KEY + "lock:" + loginUser.getUuid());
try { try {
result = (List<RouterVo>) redisTemplate.opsForValue().get(CacheConstants.ROUTE_CACHE_KEY + SecurityUtils.getUserId()); result = (List<RouterVo>) redisTemplate.opsForValue().get(CacheConstants.ROUTE_CACHE_KEY + loginUser.getUuid());
if (result == null || result.isEmpty()) { if (result == null || result.isEmpty()) {
result = menuService.selectMenuRouterByUserId(SecurityUtils.getUserId()); result = menuService.selectMenuRouterByUserId(SecurityUtils.getUserId());
if (result == null || result.isEmpty()) { if (result == null || result.isEmpty()) {
@@ -168,7 +173,7 @@ public class LoginServiceImpl implements LoginService {
// throw new CustomException("获取路由异常!"); // throw new CustomException("获取路由异常!");
return new ArrayList<>(); return new ArrayList<>();
} }
redisTemplate.opsForValue().set(CacheConstants.ROUTE_CACHE_KEY + SecurityUtils.getUserId(),result,30, TimeUnit.MINUTES); redisTemplate.opsForValue().set(CacheConstants.ROUTE_CACHE_KEY + loginUser.getUuid(), result, 30, TimeUnit.MINUTES);
} }
} finally { } finally {
if (lock.isLocked() && lock.isHeldByCurrentThread()) { if (lock.isLocked() && lock.isHeldByCurrentThread()) {

View File

@@ -14,7 +14,7 @@ public class CacheConstants {
/** /**
* 路由缓存地址 * 路由缓存地址
*/ */
public static final String ROUTE_CACHE_KEY = "router_key:"; public static final String ROUTE_CACHE_KEY = "router:key:";
/** /**
* 登录用户 redis key * 登录用户 redis key
*/ */