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

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