mirror of https://github.com/elunez/eladmin
perf: 添加权限检查,优化角色缓存及命名
parent
0a91748fd2
commit
db63c953d4
|
@ -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());
|
||||||
|
|
|
@ -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:";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据字典
|
* 数据字典
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证是否被用户关联
|
* 验证是否被用户关联
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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())) {
|
||||||
|
|
Loading…
Reference in New Issue