perf: 添加权限检查,优化角色缓存及命名

pull/872/head
Jie Zheng 2025-01-21 15:00:44 +08:00
parent 0a91748fd2
commit db63c953d4
6 changed files with 36 additions and 11 deletions

View File

@ -28,6 +28,11 @@ import java.util.stream.Collectors;
@Service(value = "el") @Service(value = "el")
public class AuthorityConfig { public class AuthorityConfig {
/**
*
* @param permissions
* @return /
*/
public Boolean check(String ...permissions){ public Boolean check(String ...permissions){
// 获取当前用户的所有权限 // 获取当前用户的所有权限
List<String> elPermissions = SecurityUtils.getCurrentUser().getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()); List<String> elPermissions = SecurityUtils.getCurrentUser().getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());

View File

@ -16,9 +16,9 @@
package me.zhengjie.utils; package me.zhengjie.utils;
/** /**
* @author: liaojinlong * @author liaojinlong
* @date: 2020/6/11 15:49 * @date 2020/6/11 15:49
* @apiNote: Key * @description Key
*/ */
public interface CacheKey { public interface CacheKey {
@ -26,31 +26,39 @@ public interface CacheKey {
* *
*/ */
String USER_ID = "user::id:"; String USER_ID = "user::id:";
/** /**
* *
*/ */
String DATA_USER = "data::user:"; String DATA_USER = "data::user:";
/** /**
* *
*/ */
String MENU_ID = "menu::id:"; String MENU_ID = "menu::id:";
String MENU_USER = "menu::user:"; String MENU_USER = "menu::user:";
/** /**
* *
*/ */
String ROLE_AUTH = "role::auth:"; String ROLE_AUTH = "role::auth:";
String ROLE_USER = "role::user:";
/** /**
* *
*/ */
String ROLE_ID = "role::id:"; String ROLE_ID = "role::id:";
/** /**
* *
*/ */
String DEPT_ID = "dept::id:"; String DEPT_ID = "dept::id:";
/** /**
* *
*/ */
String JOB_ID = "job::id:"; String JOB_ID = "job::id:";
/** /**
* *
*/ */

View File

@ -18,14 +18,15 @@ package me.zhengjie.modules.security.service;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.security.service.dto.AuthorityDto;
import me.zhengjie.modules.security.service.dto.JwtUserDto; import me.zhengjie.modules.security.service.dto.JwtUserDto;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.service.DataService; import me.zhengjie.modules.system.service.DataService;
import me.zhengjie.modules.system.service.RoleService; import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.UserService; import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.UserDto; import me.zhengjie.modules.system.service.dto.UserDto;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
/** /**
* @author Zheng Jie * @author Zheng Jie
@ -51,7 +52,10 @@ public class UserDetailsServiceImpl implements UserDetailsService {
if (!user.getEnabled()) { if (!user.getEnabled()) {
throw new BadRequestException("账号未激活!"); throw new BadRequestException("账号未激活!");
} }
jwtUserDto = new JwtUserDto(user, dataService.getDeptIds(user), roleService.buildAuthorities(user), user.getPassword()); // 获取用户的权限
List<AuthorityDto> authorities = roleService.buildPermissions(user);
// 初始化JwtUserDto
jwtUserDto = new JwtUserDto(user, dataService.getDeptIds(user), authorities, user.getPassword());
// 添加缓存数据 // 添加缓存数据
userCacheManager.addUserCache(username, jwtUserDto); userCacheManager.addUserCache(username, jwtUserDto);
} }

View File

@ -67,10 +67,10 @@ public interface RoleService {
/** /**
* ID * ID
* @param id ID * @param userId ID
* @return / * @return /
*/ */
List<RoleSmallDto> findByUsersId(Long id); List<RoleSmallDto> findByUsersId(Long userId);
/** /**
* *
@ -120,7 +120,7 @@ public interface RoleService {
* @param user * @param user
* @return * @return
*/ */
List<AuthorityDto> buildAuthorities(UserDto user); List<AuthorityDto> buildPermissions(UserDto user);
/** /**
* *

View File

@ -148,8 +148,14 @@ public class RoleServiceImpl implements RoleService {
} }
@Override @Override
public List<RoleSmallDto> findByUsersId(Long id) { public List<RoleSmallDto> findByUsersId(Long userId) {
return roleSmallMapper.toDto(new ArrayList<>(roleRepository.findByUserId(id))); String key = CacheKey.ROLE_USER + userId;
List<RoleSmallDto> roles = redisUtils.getList(key, RoleSmallDto.class);
if (CollUtil.isEmpty(roles)) {
roles = roleSmallMapper.toDto(new ArrayList<>(roleRepository.findByUserId(userId)));
redisUtils.set(key, roles, 1, TimeUnit.DAYS);
}
return roles;
} }
@Override @Override
@ -165,7 +171,7 @@ public class RoleServiceImpl implements RoleService {
} }
@Override @Override
public List<AuthorityDto> buildAuthorities(UserDto user) { public List<AuthorityDto> buildPermissions(UserDto user) {
String key = CacheKey.ROLE_AUTH + user.getId(); String key = CacheKey.ROLE_AUTH + user.getId();
List<AuthorityDto> authorityDtos = redisUtils.getList(key, AuthorityDto.class); List<AuthorityDto> authorityDtos = redisUtils.getList(key, AuthorityDto.class);
if (CollUtil.isEmpty(authorityDtos)) { if (CollUtil.isEmpty(authorityDtos)) {
@ -225,6 +231,7 @@ public class RoleServiceImpl implements RoleService {
redisUtils.delByKeys(CacheKey.DATA_USER, userIds); redisUtils.delByKeys(CacheKey.DATA_USER, userIds);
redisUtils.delByKeys(CacheKey.MENU_USER, userIds); redisUtils.delByKeys(CacheKey.MENU_USER, userIds);
redisUtils.delByKeys(CacheKey.ROLE_AUTH, userIds); redisUtils.delByKeys(CacheKey.ROLE_AUTH, userIds);
redisUtils.delByKeys(CacheKey.ROLE_USER, userIds);
} }
redisUtils.del(CacheKey.ROLE_ID + id); redisUtils.del(CacheKey.ROLE_ID + id);
} }

View File

@ -119,6 +119,7 @@ public class UserServiceImpl implements UserService {
redisUtils.del(CacheKey.DATA_USER + resources.getId()); redisUtils.del(CacheKey.DATA_USER + resources.getId());
redisUtils.del(CacheKey.MENU_USER + resources.getId()); redisUtils.del(CacheKey.MENU_USER + resources.getId());
redisUtils.del(CacheKey.ROLE_AUTH + resources.getId()); redisUtils.del(CacheKey.ROLE_AUTH + resources.getId());
redisUtils.del(CacheKey.ROLE_USER + resources.getId());
} }
// 修改部门会影响 数据权限 // 修改部门会影响 数据权限
if (!Objects.equals(resources.getDept(),user.getDept())) { if (!Objects.equals(resources.getDept(),user.getDept())) {