Merge branch 'master' of gitee.com:youlaiorg/youlai-boot
This commit is contained in:
@@ -6,7 +6,7 @@ package com.youlai.system.common.constant;
|
|||||||
* @author haoxr
|
* @author haoxr
|
||||||
* @since 2023/11/24
|
* @since 2023/11/24
|
||||||
*/
|
*/
|
||||||
public interface CacheConstants {
|
public interface SecurityConstants {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证码缓存前缀
|
* 验证码缓存前缀
|
||||||
@@ -2,8 +2,7 @@ package com.youlai.system.filter;
|
|||||||
|
|
||||||
import cn.hutool.captcha.generator.CodeGenerator;
|
import cn.hutool.captcha.generator.CodeGenerator;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.youlai.system.common.constant.CacheConstants;
|
import com.youlai.system.common.constant.SecurityConstants;
|
||||||
import com.youlai.system.security.constant.SecurityConstants;
|
|
||||||
import com.youlai.system.common.result.ResultCode;
|
import com.youlai.system.common.result.ResultCode;
|
||||||
import com.youlai.system.common.util.ResponseUtils;
|
import com.youlai.system.common.util.ResponseUtils;
|
||||||
import jakarta.servlet.FilterChain;
|
import jakarta.servlet.FilterChain;
|
||||||
@@ -25,7 +24,7 @@ import java.io.IOException;
|
|||||||
*/
|
*/
|
||||||
public class CaptchaValidationFilter extends OncePerRequestFilter {
|
public class CaptchaValidationFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
private static final AntPathRequestMatcher LOGIN_PATH_REQUEST_MATCHER = new AntPathRequestMatcher(SecurityConstants.LOGIN_PATH, "POST");
|
private static final AntPathRequestMatcher LOGIN_PATH_REQUEST_MATCHER = new AntPathRequestMatcher(com.youlai.system.security.constant.SecurityConstants.LOGIN_PATH, "POST");
|
||||||
|
|
||||||
public static final String CAPTCHA_CODE_PARAM_NAME = "captchaCode";
|
public static final String CAPTCHA_CODE_PARAM_NAME = "captchaCode";
|
||||||
public static final String CAPTCHA_KEY_PARAM_NAME = "captchaKey";
|
public static final String CAPTCHA_KEY_PARAM_NAME = "captchaKey";
|
||||||
@@ -53,7 +52,7 @@ public class CaptchaValidationFilter extends OncePerRequestFilter {
|
|||||||
}
|
}
|
||||||
// 缓存中的验证码
|
// 缓存中的验证码
|
||||||
String verifyCodeKey = request.getParameter(CAPTCHA_KEY_PARAM_NAME);
|
String verifyCodeKey = request.getParameter(CAPTCHA_KEY_PARAM_NAME);
|
||||||
String cacheVerifyCode = (String) redisTemplate.opsForValue().get(CacheConstants.CAPTCHA_CODE_PREFIX + verifyCodeKey);
|
String cacheVerifyCode = (String) redisTemplate.opsForValue().get(SecurityConstants.CAPTCHA_CODE_PREFIX + verifyCodeKey);
|
||||||
if (cacheVerifyCode == null) {
|
if (cacheVerifyCode == null) {
|
||||||
ResponseUtils.writeErrMsg(response, ResultCode.VERIFY_CODE_TIMEOUT);
|
ResponseUtils.writeErrMsg(response, ResultCode.VERIFY_CODE_TIMEOUT);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package com.youlai.system.filter;
|
|||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.jwt.JWTPayload;
|
import cn.hutool.jwt.JWTPayload;
|
||||||
import com.youlai.system.common.constant.CacheConstants;
|
import com.youlai.system.common.constant.SecurityConstants;
|
||||||
import com.youlai.system.common.result.ResultCode;
|
import com.youlai.system.common.result.ResultCode;
|
||||||
import com.youlai.system.security.util.JwtUtils;
|
import com.youlai.system.security.util.JwtUtils;
|
||||||
import com.youlai.system.common.util.ResponseUtils;
|
import com.youlai.system.common.util.ResponseUtils;
|
||||||
@@ -49,7 +49,7 @@ public class JwtValidationFilter extends OncePerRequestFilter {
|
|||||||
Map<String, Object> payload = JwtUtils.parseToken(token);
|
Map<String, Object> payload = JwtUtils.parseToken(token);
|
||||||
|
|
||||||
String jti = Convert.toStr(payload.get(JWTPayload.JWT_ID));
|
String jti = Convert.toStr(payload.get(JWTPayload.JWT_ID));
|
||||||
Boolean isTokenBlacklisted = redisTemplate.hasKey(CacheConstants.BLACKLIST_TOKEN_PREFIX + jti);
|
Boolean isTokenBlacklisted = redisTemplate.hasKey(SecurityConstants.BLACKLIST_TOKEN_PREFIX + jti);
|
||||||
if (Boolean.TRUE.equals(isTokenBlacklisted)) {
|
if (Boolean.TRUE.equals(isTokenBlacklisted)) {
|
||||||
ResponseUtils.writeErrMsg(response, ResultCode.TOKEN_INVALID);
|
ResponseUtils.writeErrMsg(response, ResultCode.TOKEN_INVALID);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import org.apache.ibatis.annotations.Mapper;
|
|||||||
public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {
|
public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 统计角色下绑定的用户数量
|
* 获取角色绑定的用户数
|
||||||
*
|
*
|
||||||
* @param roleId 角色ID
|
* @param roleId 角色ID
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -2,11 +2,8 @@ package com.youlai.system.security.service;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.youlai.system.common.constant.CacheConstants;
|
import com.youlai.system.common.constant.SecurityConstants;
|
||||||
import com.youlai.system.security.util.SecurityUtils;
|
import com.youlai.system.security.util.SecurityUtils;
|
||||||
import com.youlai.system.model.bo.RolePermsBO;
|
|
||||||
import com.youlai.system.service.SysRoleMenuService;
|
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
@@ -28,102 +25,6 @@ public class PermissionService {
|
|||||||
|
|
||||||
private final RedisTemplate<String, Object> redisTemplate;
|
private final RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
private final SysRoleMenuService roleMenuService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化权限缓存
|
|
||||||
*/
|
|
||||||
@PostConstruct
|
|
||||||
public void initRolePermsCache() {
|
|
||||||
refreshRolePermsCache();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 刷新权限缓存
|
|
||||||
*/
|
|
||||||
public void refreshRolePermsCache() {
|
|
||||||
// 清理权限缓存
|
|
||||||
redisTemplate.opsForHash().delete(CacheConstants.ROLE_PERMS_PREFIX, "*");
|
|
||||||
|
|
||||||
List<RolePermsBO> list = roleMenuService.getRolePermsList(null);
|
|
||||||
if (CollectionUtil.isNotEmpty(list)) {
|
|
||||||
list.forEach(item -> {
|
|
||||||
String roleCode = item.getRoleCode();
|
|
||||||
Set<String> perms = item.getPerms();
|
|
||||||
redisTemplate.opsForHash().put(CacheConstants.ROLE_PERMS_PREFIX, roleCode, perms);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 刷新权限缓存
|
|
||||||
*/
|
|
||||||
public void refreshRolePermsCache(String roleCode) {
|
|
||||||
// 清理权限缓存
|
|
||||||
redisTemplate.opsForHash().delete(CacheConstants.ROLE_PERMS_PREFIX, roleCode);
|
|
||||||
|
|
||||||
List<RolePermsBO> list = roleMenuService.getRolePermsList(roleCode);
|
|
||||||
if (CollectionUtil.isNotEmpty(list)) {
|
|
||||||
RolePermsBO rolePerms = list.get(0);
|
|
||||||
if (rolePerms == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Set<String> perms = rolePerms.getPerms();
|
|
||||||
redisTemplate.opsForHash().put(CacheConstants.ROLE_PERMS_PREFIX, roleCode, perms);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 刷新权限缓存 (角色编码变更时调用)
|
|
||||||
*/
|
|
||||||
public void refreshRolePermsCache(String oldRoleCode, String newRoleCode) {
|
|
||||||
// 清理旧角色权限缓存
|
|
||||||
redisTemplate.opsForHash().delete(CacheConstants.ROLE_PERMS_PREFIX, oldRoleCode);
|
|
||||||
|
|
||||||
// 添加新角色权限缓存
|
|
||||||
List<RolePermsBO> list = roleMenuService.getRolePermsList(newRoleCode);
|
|
||||||
if (CollectionUtil.isNotEmpty(list)) {
|
|
||||||
RolePermsBO rolePerms = list.get(0);
|
|
||||||
if (rolePerms == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Set<String> perms = rolePerms.getPerms();
|
|
||||||
redisTemplate.opsForHash().put(CacheConstants.ROLE_PERMS_PREFIX, newRoleCode, perms);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取角色权限列表
|
|
||||||
*
|
|
||||||
* @param roleCodes 角色编码集合
|
|
||||||
* @return 角色权限列表
|
|
||||||
*/
|
|
||||||
public Set<String> getRolePermsFormCache(Set<String> roleCodes) {
|
|
||||||
// 检查输入是否为空
|
|
||||||
if (CollectionUtil.isEmpty(roleCodes)) {
|
|
||||||
return Collections.emptySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
Set<String> perms = new HashSet<>();
|
|
||||||
// 从缓存中一次性获取所有角色的权限
|
|
||||||
Collection<Object> roleCodesAsObjects = new ArrayList<>(roleCodes);
|
|
||||||
List<Object> rolePermsList = redisTemplate.opsForHash().multiGet(CacheConstants.ROLE_PERMS_PREFIX, roleCodesAsObjects);
|
|
||||||
|
|
||||||
for (Object rolePermsObj : rolePermsList) {
|
|
||||||
if (rolePermsObj instanceof Set) {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Set<String> rolePerms = (Set<String>) rolePermsObj;
|
|
||||||
perms.addAll(rolePerms);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return perms;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断当前登录用户是否拥有操作权限
|
* 判断当前登录用户是否拥有操作权限
|
||||||
*
|
*
|
||||||
@@ -165,4 +66,32 @@ public class PermissionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从缓存中获取角色权限列表
|
||||||
|
*
|
||||||
|
* @param roleCodes 角色编码集合
|
||||||
|
* @return 角色权限列表
|
||||||
|
*/
|
||||||
|
public Set<String> getRolePermsFormCache(Set<String> roleCodes) {
|
||||||
|
// 检查输入是否为空
|
||||||
|
if (CollectionUtil.isEmpty(roleCodes)) {
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> perms = new HashSet<>();
|
||||||
|
// 从缓存中一次性获取所有角色的权限
|
||||||
|
Collection<Object> roleCodesAsObjects = new ArrayList<>(roleCodes);
|
||||||
|
List<Object> rolePermsList = redisTemplate.opsForHash().multiGet(SecurityConstants.ROLE_PERMS_PREFIX, roleCodesAsObjects);
|
||||||
|
|
||||||
|
for (Object rolePermsObj : rolePermsList) {
|
||||||
|
if (rolePermsObj instanceof Set) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Set<String> rolePerms = (Set<String>) rolePermsObj;
|
||||||
|
perms.addAll(rolePerms);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return perms;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public class SecurityUtils {
|
|||||||
/**
|
/**
|
||||||
* 获取当前登录人信息
|
* 获取当前登录人信息
|
||||||
*
|
*
|
||||||
* @return
|
* @return SysUserDetails
|
||||||
*/
|
*/
|
||||||
public static SysUserDetails getUser() {
|
public static SysUserDetails getUser() {
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
@@ -36,7 +36,7 @@ public class SecurityUtils {
|
|||||||
/**
|
/**
|
||||||
* 获取用户ID
|
* 获取用户ID
|
||||||
*
|
*
|
||||||
* @return
|
* @return Long
|
||||||
*/
|
*/
|
||||||
public static Long getUserId() {
|
public static Long getUserId() {
|
||||||
Long userId = Convert.toLong(getUser().getUserId());
|
Long userId = Convert.toLong(getUser().getUserId());
|
||||||
@@ -65,7 +65,7 @@ public class SecurityUtils {
|
|||||||
/**
|
/**
|
||||||
* 获取用户角色集合
|
* 获取用户角色集合
|
||||||
*
|
*
|
||||||
* @return
|
* @return 角色集合
|
||||||
*/
|
*/
|
||||||
public static Set<String> getRoles() {
|
public static Set<String> getRoles() {
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
@@ -80,25 +80,6 @@ public class SecurityUtils {
|
|||||||
return Collections.EMPTY_SET;
|
return Collections.EMPTY_SET;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取用户权限集合
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static Set<String> getPerms() {
|
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
||||||
if (authentication != null) {
|
|
||||||
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
|
||||||
if (CollectionUtil.isNotEmpty(authorities)) {
|
|
||||||
return authorities.stream()
|
|
||||||
.map(GrantedAuthority::getAuthority)
|
|
||||||
.filter(authority -> !authority.startsWith("ROLE_"))
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Collections.EMPTY_SET;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否超级管理员
|
* 是否超级管理员
|
||||||
* <p>
|
* <p>
|
||||||
@@ -111,23 +92,4 @@ public class SecurityUtils {
|
|||||||
return roles.contains(SystemConstants.ROOT_ROLE_CODE);
|
return roles.contains(SystemConstants.ROOT_ROLE_CODE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否拥有权限判断
|
|
||||||
* <p>
|
|
||||||
* 适用业务判断(接口权限判断适用Spring Security 自带注解 PreAuthorize 判断即可 )
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static boolean hasPerm(String perm) {
|
|
||||||
|
|
||||||
if (isRoot()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Set<String> perms = getPerms();
|
|
||||||
|
|
||||||
return perms.stream().anyMatch(item -> PatternMatchUtils.simpleMatch(perm, item));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,9 @@ package com.youlai.system.service;
|
|||||||
|
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.youlai.system.model.bo.RolePermsBO;
|
|
||||||
import com.youlai.system.model.entity.SysRoleMenu;
|
import com.youlai.system.model.entity.SysRoleMenu;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色菜单业务接口
|
* 角色菜单业务接口
|
||||||
@@ -26,9 +24,23 @@ public interface SysRoleMenuService extends IService<SysRoleMenu> {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取角色和权限的列表
|
* 刷新权限缓存(所有角色)
|
||||||
*
|
|
||||||
* @return 角色权限的列表
|
|
||||||
*/
|
*/
|
||||||
List<RolePermsBO> getRolePermsList(String roleCode);
|
void refreshRolePermsCache();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新权限缓存(指定角色)
|
||||||
|
*
|
||||||
|
* @param roleCode 角色编码
|
||||||
|
*/
|
||||||
|
void refreshRolePermsCache(String roleCode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新权限缓存(修改角色编码时调用)
|
||||||
|
*
|
||||||
|
* @param oldRoleCode 旧角色编码
|
||||||
|
* @param newRoleCode 新角色编码
|
||||||
|
*/
|
||||||
|
void refreshRolePermsCache(String oldRoleCode, String newRoleCode);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,5 +23,5 @@ public interface SysUserRoleService extends IService<SysUserRole> {
|
|||||||
* @param roleId 角色ID
|
* @param roleId 角色ID
|
||||||
* @return true:已分配 false:未分配
|
* @return true:已分配 false:未分配
|
||||||
*/
|
*/
|
||||||
boolean isRoleAssignedToUser(Long roleId);
|
boolean hasAssignedUsers(Long roleId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import cn.hutool.core.convert.Convert;
|
|||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.jwt.JWTPayload;
|
import cn.hutool.jwt.JWTPayload;
|
||||||
import com.youlai.system.common.constant.CacheConstants;
|
import com.youlai.system.common.constant.SecurityConstants;
|
||||||
import com.youlai.system.common.enums.CaptchaTypeEnum;
|
import com.youlai.system.common.enums.CaptchaTypeEnum;
|
||||||
import com.youlai.system.model.dto.CaptchaResult;
|
import com.youlai.system.model.dto.CaptchaResult;
|
||||||
import com.youlai.system.model.dto.LoginResult;
|
import com.youlai.system.model.dto.LoginResult;
|
||||||
@@ -81,9 +81,9 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
Long expiration = Convert.toLong(claims.get(JWTPayload.EXPIRES_AT));
|
Long expiration = Convert.toLong(claims.get(JWTPayload.EXPIRES_AT));
|
||||||
if (expiration != null) {
|
if (expiration != null) {
|
||||||
long ttl = expiration - System.currentTimeMillis() / 1000;
|
long ttl = expiration - System.currentTimeMillis() / 1000;
|
||||||
redisTemplate.opsForValue().set(CacheConstants.BLACKLIST_TOKEN_PREFIX + jti, null, ttl, TimeUnit.SECONDS);
|
redisTemplate.opsForValue().set(SecurityConstants.BLACKLIST_TOKEN_PREFIX + jti, null, ttl, TimeUnit.SECONDS);
|
||||||
} else {
|
} else {
|
||||||
redisTemplate.opsForValue().set(CacheConstants.BLACKLIST_TOKEN_PREFIX + jti, null);
|
redisTemplate.opsForValue().set(SecurityConstants.BLACKLIST_TOKEN_PREFIX + jti, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SecurityContextHolder.clearContext();
|
SecurityContextHolder.clearContext();
|
||||||
@@ -124,7 +124,7 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
|
|
||||||
// 验证码文本缓存至Redis,用于登录校验
|
// 验证码文本缓存至Redis,用于登录校验
|
||||||
String captchaKey = IdUtil.fastSimpleUUID();
|
String captchaKey = IdUtil.fastSimpleUUID();
|
||||||
redisTemplate.opsForValue().set(CacheConstants.CAPTCHA_CODE_PREFIX + captchaKey, captchaCode,
|
redisTemplate.opsForValue().set(SecurityConstants.CAPTCHA_CODE_PREFIX + captchaKey, captchaCode,
|
||||||
captchaProperties.getExpireSeconds(), TimeUnit.SECONDS);
|
captchaProperties.getExpireSeconds(), TimeUnit.SECONDS);
|
||||||
|
|
||||||
return CaptchaResult.builder()
|
return CaptchaResult.builder()
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ import com.youlai.system.model.form.MenuForm;
|
|||||||
import com.youlai.system.model.query.MenuQuery;
|
import com.youlai.system.model.query.MenuQuery;
|
||||||
import com.youlai.system.model.vo.MenuVO;
|
import com.youlai.system.model.vo.MenuVO;
|
||||||
import com.youlai.system.model.vo.RouteVO;
|
import com.youlai.system.model.vo.RouteVO;
|
||||||
import com.youlai.system.security.service.PermissionService;
|
|
||||||
import com.youlai.system.service.SysMenuService;
|
import com.youlai.system.service.SysMenuService;
|
||||||
|
import com.youlai.system.service.SysRoleMenuService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.cache.annotation.CacheEvict;
|
import org.springframework.cache.annotation.CacheEvict;
|
||||||
@@ -43,7 +43,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
|
|
||||||
private final MenuConverter menuConverter;
|
private final MenuConverter menuConverter;
|
||||||
|
|
||||||
private final PermissionService permissionService;
|
private final SysRoleMenuService roleMenuService;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -224,7 +224,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
if (result) {
|
if (result) {
|
||||||
// 编辑刷新角色权限缓存
|
// 编辑刷新角色权限缓存
|
||||||
if (menuForm.getId() != null) {
|
if (menuForm.getId() != null) {
|
||||||
permissionService.refreshRolePermsCache();
|
roleMenuService.refreshRolePermsCache();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@@ -251,7 +251,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
*
|
*
|
||||||
* @param menuId 菜单ID
|
* @param menuId 菜单ID
|
||||||
* @param visible 是否显示(1->显示;2->隐藏)
|
* @param visible 是否显示(1->显示;2->隐藏)
|
||||||
* @return
|
* @return 是否修改成功
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@CacheEvict(cacheNames = "menu", key = "'routes'")
|
@CacheEvict(cacheNames = "menu", key = "'routes'")
|
||||||
@@ -302,7 +302,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
|||||||
|
|
||||||
// 刷新角色权限缓存
|
// 刷新角色权限缓存
|
||||||
if (result) {
|
if (result) {
|
||||||
permissionService.refreshRolePermsCache();
|
roleMenuService.refreshRolePermsCache();
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,19 @@
|
|||||||
package com.youlai.system.service.impl;
|
package com.youlai.system.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.youlai.system.common.constant.SecurityConstants;
|
||||||
import com.youlai.system.mapper.SysRoleMenuMapper;
|
import com.youlai.system.mapper.SysRoleMenuMapper;
|
||||||
import com.youlai.system.model.bo.RolePermsBO;
|
import com.youlai.system.model.bo.RolePermsBO;
|
||||||
import com.youlai.system.model.entity.SysRoleMenu;
|
import com.youlai.system.model.entity.SysRoleMenu;
|
||||||
import com.youlai.system.service.SysRoleMenuService;
|
import com.youlai.system.service.SysRoleMenuService;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,8 +23,79 @@ import java.util.List;
|
|||||||
* @since 2.5.0
|
* @since 2.5.0
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {
|
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {
|
||||||
|
|
||||||
|
private final RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化权限缓存
|
||||||
|
*/
|
||||||
|
@PostConstruct
|
||||||
|
public void initRolePermsCache() {
|
||||||
|
refreshRolePermsCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新权限缓存
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void refreshRolePermsCache() {
|
||||||
|
// 清理权限缓存
|
||||||
|
redisTemplate.opsForHash().delete(SecurityConstants.ROLE_PERMS_PREFIX, "*");
|
||||||
|
|
||||||
|
List<RolePermsBO> list = this.baseMapper.getRolePermsList(null);
|
||||||
|
if (CollectionUtil.isNotEmpty(list)) {
|
||||||
|
list.forEach(item -> {
|
||||||
|
String roleCode = item.getRoleCode();
|
||||||
|
Set<String> perms = item.getPerms();
|
||||||
|
redisTemplate.opsForHash().put(SecurityConstants.ROLE_PERMS_PREFIX, roleCode, perms);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新权限缓存
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void refreshRolePermsCache(String roleCode) {
|
||||||
|
// 清理权限缓存
|
||||||
|
redisTemplate.opsForHash().delete(SecurityConstants.ROLE_PERMS_PREFIX, roleCode);
|
||||||
|
|
||||||
|
List<RolePermsBO> list = this.baseMapper.getRolePermsList(roleCode);
|
||||||
|
if (CollectionUtil.isNotEmpty(list)) {
|
||||||
|
RolePermsBO rolePerms = list.get(0);
|
||||||
|
if (rolePerms == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> perms = rolePerms.getPerms();
|
||||||
|
redisTemplate.opsForHash().put(SecurityConstants.ROLE_PERMS_PREFIX, roleCode, perms);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新权限缓存 (角色编码变更时调用)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void refreshRolePermsCache(String oldRoleCode, String newRoleCode) {
|
||||||
|
// 清理旧角色权限缓存
|
||||||
|
redisTemplate.opsForHash().delete(SecurityConstants.ROLE_PERMS_PREFIX, oldRoleCode);
|
||||||
|
|
||||||
|
// 添加新角色权限缓存
|
||||||
|
List<RolePermsBO> list =this.baseMapper.getRolePermsList(newRoleCode);
|
||||||
|
if (CollectionUtil.isNotEmpty(list)) {
|
||||||
|
RolePermsBO rolePerms = list.get(0);
|
||||||
|
if (rolePerms == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> perms = rolePerms.getPerms();
|
||||||
|
redisTemplate.opsForHash().put(SecurityConstants.ROLE_PERMS_PREFIX, newRoleCode, perms);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取角色拥有的菜单ID集合
|
* 获取角色拥有的菜单ID集合
|
||||||
*
|
*
|
||||||
@@ -31,15 +108,4 @@ public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRo
|
|||||||
return menuIds;
|
return menuIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取权限角色列表
|
|
||||||
*
|
|
||||||
* @return 权限角色列表
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public List<RolePermsBO> getRolePermsList(String roleCode) {
|
|
||||||
List<RolePermsBO> rolePerms= this.baseMapper.getRolePermsList(roleCode);
|
|
||||||
return rolePerms;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,6 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||||||
private final SysRoleMenuService roleMenuService;
|
private final SysRoleMenuService roleMenuService;
|
||||||
private final SysUserRoleService userRoleService;
|
private final SysUserRoleService userRoleService;
|
||||||
private final RoleConverter roleConverter;
|
private final RoleConverter roleConverter;
|
||||||
private final PermissionService permissionService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色分页列表
|
* 角色分页列表
|
||||||
@@ -129,7 +128,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||||||
!StrUtil.equals(oldRole.getCode(), roleCode) ||
|
!StrUtil.equals(oldRole.getCode(), roleCode) ||
|
||||||
!ObjectUtil.equals(oldRole.getStatus(), roleForm.getStatus())
|
!ObjectUtil.equals(oldRole.getStatus(), roleForm.getStatus())
|
||||||
)) {
|
)) {
|
||||||
permissionService.refreshRolePermsCache(oldRole.getCode(), roleCode);
|
roleMenuService.refreshRolePermsCache(oldRole.getCode(), roleCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@@ -160,10 +159,11 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||||||
SysRole role = this.getById(roleId);
|
SysRole role = this.getById(roleId);
|
||||||
Assert.isTrue(role != null, "角色不存在");
|
Assert.isTrue(role != null, "角色不存在");
|
||||||
|
|
||||||
|
role.setStatus(status);
|
||||||
boolean result = this.updateById(role);
|
boolean result = this.updateById(role);
|
||||||
if (result) {
|
if (result) {
|
||||||
// 刷新角色的权限缓存
|
// 刷新角色的权限缓存
|
||||||
permissionService.refreshRolePermsCache(role.getCode());
|
roleMenuService.refreshRolePermsCache(role.getCode());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -186,13 +186,13 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||||||
Assert.isTrue(role != null, "角色不存在");
|
Assert.isTrue(role != null, "角色不存在");
|
||||||
|
|
||||||
// 判断角色是否被用户关联
|
// 判断角色是否被用户关联
|
||||||
boolean isRoleAssigned = userRoleService.isRoleAssignedToUser(roleId);
|
boolean isRoleAssigned = userRoleService.hasAssignedUsers(roleId);
|
||||||
Assert.isTrue(!isRoleAssigned, "角色【{}】已分配用户,请先解除关联后删除", role.getName());
|
Assert.isTrue(!isRoleAssigned, "角色【{}】已分配用户,请先解除关联后删除", role.getName());
|
||||||
|
|
||||||
boolean deleteResult = this.removeById(roleId);
|
boolean deleteResult = this.removeById(roleId);
|
||||||
if (deleteResult) {
|
if (deleteResult) {
|
||||||
// 删除成功,刷新权限缓存
|
// 删除成功,刷新权限缓存
|
||||||
permissionService.refreshRolePermsCache(role.getCode());
|
roleMenuService.refreshRolePermsCache(role.getCode());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -238,7 +238,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 刷新角色的权限缓存
|
// 刷新角色的权限缓存
|
||||||
permissionService.refreshRolePermsCache(role.getCode());
|
roleMenuService.refreshRolePermsCache(role.getCode());
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
|
|||||||
* @return true:已分配 false:未分配
|
* @return true:已分配 false:未分配
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean isRoleAssignedToUser(Long roleId) {
|
public boolean hasAssignedUsers(Long roleId) {
|
||||||
int count = this.baseMapper.countUsersForRole(roleId);
|
int count = this.baseMapper.countUsersForRole(roleId);
|
||||||
return count > 0;
|
return count > 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,13 +14,13 @@
|
|||||||
user_id = #{userId}
|
user_id = #{userId}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<!-- 统计角色下绑定的用户数量 -->
|
<!-- 获取角色绑定的用户数 -->
|
||||||
<select id="countUsersForRole" resultType="java.lang.Integer">
|
<select id="countUsersForRole" resultType="java.lang.Integer">
|
||||||
SELECT
|
SELECT
|
||||||
count(*)
|
count(*)
|
||||||
FROM
|
FROM
|
||||||
sys_user_role t1
|
sys_user_role t1
|
||||||
INNER JOIN sys_role t2 ON t1.role_id = t2.id AND t2.deleted = 0
|
INNER JOIN sys_role t2 ON t1.role_id = t2.id AND t2.deleted = 0
|
||||||
INNER JOIN sys_user t3 ON t1.user_id = t3.id
|
INNER JOIN sys_user t3 ON t1.user_id = t3.id
|
||||||
AND t3.deleted = 0
|
AND t3.deleted = 0
|
||||||
WHERE
|
WHERE
|
||||||
|
|||||||
Reference in New Issue
Block a user