Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
有来开源组织
youlai-boot
提交
93720b52
Y
youlai-boot
项目概览
有来开源组织
/
youlai-boot
通知
41
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
youlai-boot
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
93720b52
编写于
1月 29, 2024
作者:
H
haoxr
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor: 刷新角色权限代码位置调整
上级
825f1fdf
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
146 addition
and
178 deletion
+146
-178
src/main/java/com/youlai/system/common/constant/SecurityConstants.java
.../com/youlai/system/common/constant/SecurityConstants.java
+1
-1
src/main/java/com/youlai/system/filter/CaptchaValidationFilter.java
...ava/com/youlai/system/filter/CaptchaValidationFilter.java
+3
-4
src/main/java/com/youlai/system/filter/JwtValidationFilter.java
...in/java/com/youlai/system/filter/JwtValidationFilter.java
+2
-2
src/main/java/com/youlai/system/mapper/SysUserRoleMapper.java
...main/java/com/youlai/system/mapper/SysUserRoleMapper.java
+1
-1
src/main/java/com/youlai/system/security/service/PermissionService.java
...com/youlai/system/security/service/PermissionService.java
+29
-100
src/main/java/com/youlai/system/security/util/SecurityUtils.java
...n/java/com/youlai/system/security/util/SecurityUtils.java
+3
-41
src/main/java/com/youlai/system/service/SysRoleMenuService.java
...in/java/com/youlai/system/service/SysRoleMenuService.java
+17
-5
src/main/java/com/youlai/system/service/SysUserRoleService.java
...in/java/com/youlai/system/service/SysUserRoleService.java
+1
-1
src/main/java/com/youlai/system/service/impl/AuthServiceImpl.java
.../java/com/youlai/system/service/impl/AuthServiceImpl.java
+4
-4
src/main/java/com/youlai/system/service/impl/SysMenuServiceImpl.java
...va/com/youlai/system/service/impl/SysMenuServiceImpl.java
+5
-5
src/main/java/com/youlai/system/service/impl/SysRoleMenuServiceImpl.java
...om/youlai/system/service/impl/SysRoleMenuServiceImpl.java
+77
-11
src/main/java/com/youlai/system/service/impl/SysUserRoleServiceImpl.java
...om/youlai/system/service/impl/SysUserRoleServiceImpl.java
+1
-1
src/main/resources/mapper/SysUserRoleMapper.xml
src/main/resources/mapper/SysUserRoleMapper.xml
+2
-2
未找到文件。
src/main/java/com/youlai/system/common/constant/
Cache
Constants.java
→
src/main/java/com/youlai/system/common/constant/
Security
Constants.java
浏览文件 @
93720b52
...
...
@@ -6,7 +6,7 @@ package com.youlai.system.common.constant;
* @author haoxr
* @since 2023/11/24
*/
public
interface
Cache
Constants
{
public
interface
Security
Constants
{
/**
* 验证码缓存前缀
...
...
src/main/java/com/youlai/system/filter/CaptchaValidationFilter.java
浏览文件 @
93720b52
...
...
@@ -2,8 +2,7 @@ package com.youlai.system.filter;
import
cn.hutool.captcha.generator.CodeGenerator
;
import
cn.hutool.core.util.StrUtil
;
import
com.youlai.system.common.constant.CacheConstants
;
import
com.youlai.system.security.constant.SecurityConstants
;
import
com.youlai.system.common.constant.SecurityConstants
;
import
com.youlai.system.common.result.ResultCode
;
import
com.youlai.system.common.util.ResponseUtils
;
import
jakarta.servlet.FilterChain
;
...
...
@@ -25,7 +24,7 @@ import java.io.IOException;
*/
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_KEY_PARAM_NAME
=
"captchaKey"
;
...
...
@@ -53,7 +52,7 @@ public class CaptchaValidationFilter extends OncePerRequestFilter {
}
// 缓存中的验证码
String
verifyCodeKey
=
request
.
getParameter
(
CAPTCHA_KEY_PARAM_NAME
);
String
cacheVerifyCode
=
(
String
)
redisTemplate
.
opsForValue
().
get
(
Cache
Constants
.
CAPTCHA_CODE_PREFIX
+
verifyCodeKey
);
String
cacheVerifyCode
=
(
String
)
redisTemplate
.
opsForValue
().
get
(
Security
Constants
.
CAPTCHA_CODE_PREFIX
+
verifyCodeKey
);
if
(
cacheVerifyCode
==
null
)
{
ResponseUtils
.
writeErrMsg
(
response
,
ResultCode
.
VERIFY_CODE_TIMEOUT
);
}
else
{
...
...
src/main/java/com/youlai/system/filter/JwtValidationFilter.java
浏览文件 @
93720b52
...
...
@@ -3,7 +3,7 @@ package com.youlai.system.filter;
import
cn.hutool.core.convert.Convert
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.jwt.JWTPayload
;
import
com.youlai.system.common.constant.
Cache
Constants
;
import
com.youlai.system.common.constant.
Security
Constants
;
import
com.youlai.system.common.result.ResultCode
;
import
com.youlai.system.security.util.JwtUtils
;
import
com.youlai.system.common.util.ResponseUtils
;
...
...
@@ -49,7 +49,7 @@ public class JwtValidationFilter extends OncePerRequestFilter {
Map
<
String
,
Object
>
payload
=
JwtUtils
.
parseToken
(
token
);
String
jti
=
Convert
.
toStr
(
payload
.
get
(
JWTPayload
.
JWT_ID
));
Boolean
isTokenBlacklisted
=
redisTemplate
.
hasKey
(
Cache
Constants
.
BLACKLIST_TOKEN_PREFIX
+
jti
);
Boolean
isTokenBlacklisted
=
redisTemplate
.
hasKey
(
Security
Constants
.
BLACKLIST_TOKEN_PREFIX
+
jti
);
if
(
Boolean
.
TRUE
.
equals
(
isTokenBlacklisted
))
{
ResponseUtils
.
writeErrMsg
(
response
,
ResultCode
.
TOKEN_INVALID
);
return
;
...
...
src/main/java/com/youlai/system/mapper/SysUserRoleMapper.java
浏览文件 @
93720b52
...
...
@@ -14,7 +14,7 @@ import org.apache.ibatis.annotations.Mapper;
public
interface
SysUserRoleMapper
extends
BaseMapper
<
SysUserRole
>
{
/**
*
统计角色下绑定的用户数量
*
获取角色绑定的用户数
*
* @param roleId 角色ID
*/
...
...
src/main/java/com/youlai/system/security/service/PermissionService.java
浏览文件 @
93720b52
...
...
@@ -2,11 +2,8 @@ package com.youlai.system.security.service;
import
cn.hutool.core.collection.CollectionUtil
;
import
cn.hutool.core.util.StrUtil
;
import
com.youlai.system.common.constant.
Cache
Constants
;
import
com.youlai.system.common.constant.
Security
Constants
;
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.extern.slf4j.Slf4j
;
import
org.springframework.data.redis.core.RedisTemplate
;
...
...
@@ -28,102 +25,6 @@ public class PermissionService {
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
;
}
}
src/main/java/com/youlai/system/security/util/SecurityUtils.java
浏览文件 @
93720b52
...
...
@@ -20,7 +20,7 @@ public class SecurityUtils {
/**
* 获取当前登录人信息
*
* @return
* @return
SysUserDetails
*/
public
static
SysUserDetails
getUser
()
{
Authentication
authentication
=
SecurityContextHolder
.
getContext
().
getAuthentication
();
...
...
@@ -36,7 +36,7 @@ public class SecurityUtils {
/**
* 获取用户ID
*
* @return
* @return
Long
*/
public
static
Long
getUserId
()
{
Long
userId
=
Convert
.
toLong
(
getUser
().
getUserId
());
...
...
@@ -65,7 +65,7 @@ public class SecurityUtils {
/**
* 获取用户角色集合
*
* @return
* @return
角色集合
*/
public
static
Set
<
String
>
getRoles
()
{
Authentication
authentication
=
SecurityContextHolder
.
getContext
().
getAuthentication
();
...
...
@@ -80,25 +80,6 @@ public class SecurityUtils {
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>
...
...
@@ -111,23 +92,4 @@ public class SecurityUtils {
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
));
}
}
src/main/java/com/youlai/system/service/SysRoleMenuService.java
浏览文件 @
93720b52
...
...
@@ -2,11 +2,9 @@ package com.youlai.system.service;
import
com.baomidou.mybatisplus.extension.service.IService
;
import
com.youlai.system.model.bo.RolePermsBO
;
import
com.youlai.system.model.entity.SysRoleMenu
;
import
java.util.List
;
import
java.util.Set
;
/**
* 角色菜单业务接口
...
...
@@ -26,9 +24,23 @@ public interface SysRoleMenuService extends IService<SysRoleMenu> {
/**
* 获取角色和权限的列表
* 刷新权限缓存(所有角色)
*/
void
refreshRolePermsCache
();
/**
* 刷新权限缓存(指定角色)
*
* @param roleCode 角色编码
*/
void
refreshRolePermsCache
(
String
roleCode
);
/**
* 刷新权限缓存(修改角色编码时调用)
*
* @return 角色权限的列表
* @param oldRoleCode 旧角色编码
* @param newRoleCode 新角色编码
*/
List
<
RolePermsBO
>
getRolePermsList
(
String
roleCode
);
void
refreshRolePermsCache
(
String
oldRoleCode
,
String
newRoleCode
);
}
src/main/java/com/youlai/system/service/SysUserRoleService.java
浏览文件 @
93720b52
...
...
@@ -23,5 +23,5 @@ public interface SysUserRoleService extends IService<SysUserRole> {
* @param roleId 角色ID
* @return true:已分配 false:未分配
*/
boolean
isRoleAssignedToUser
(
Long
roleId
);
boolean
hasAssignedUsers
(
Long
roleId
);
}
src/main/java/com/youlai/system/service/impl/AuthServiceImpl.java
浏览文件 @
93720b52
...
...
@@ -7,7 +7,7 @@ import cn.hutool.core.convert.Convert;
import
cn.hutool.core.util.IdUtil
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.jwt.JWTPayload
;
import
com.youlai.system.common.constant.
Cache
Constants
;
import
com.youlai.system.common.constant.
Security
Constants
;
import
com.youlai.system.common.enums.CaptchaTypeEnum
;
import
com.youlai.system.model.dto.CaptchaResult
;
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
));
if
(
expiration
!=
null
)
{
long
ttl
=
expiration
-
System
.
currentTimeMillis
()
/
1000
;
redisTemplate
.
opsForValue
().
set
(
Cache
Constants
.
BLACKLIST_TOKEN_PREFIX
+
jti
,
null
,
ttl
,
TimeUnit
.
SECONDS
);
redisTemplate
.
opsForValue
().
set
(
Security
Constants
.
BLACKLIST_TOKEN_PREFIX
+
jti
,
null
,
ttl
,
TimeUnit
.
SECONDS
);
}
else
{
redisTemplate
.
opsForValue
().
set
(
Cache
Constants
.
BLACKLIST_TOKEN_PREFIX
+
jti
,
null
);
redisTemplate
.
opsForValue
().
set
(
Security
Constants
.
BLACKLIST_TOKEN_PREFIX
+
jti
,
null
);
}
}
SecurityContextHolder
.
clearContext
();
...
...
@@ -124,7 +124,7 @@ public class AuthServiceImpl implements AuthService {
// 验证码文本缓存至Redis,用于登录校验
String
captchaKey
=
IdUtil
.
fastSimpleUUID
();
redisTemplate
.
opsForValue
().
set
(
Cache
Constants
.
CAPTCHA_CODE_PREFIX
+
captchaKey
,
captchaCode
,
redisTemplate
.
opsForValue
().
set
(
Security
Constants
.
CAPTCHA_CODE_PREFIX
+
captchaKey
,
captchaCode
,
captchaProperties
.
getExpireSeconds
(),
TimeUnit
.
SECONDS
);
return
CaptchaResult
.
builder
()
...
...
src/main/java/com/youlai/system/service/impl/SysMenuServiceImpl.java
浏览文件 @
93720b52
...
...
@@ -18,8 +18,8 @@ import com.youlai.system.model.form.MenuForm;
import
com.youlai.system.model.query.MenuQuery
;
import
com.youlai.system.model.vo.MenuVO
;
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.SysRoleMenuService
;
import
lombok.RequiredArgsConstructor
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.cache.annotation.CacheEvict
;
...
...
@@ -43,7 +43,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
private
final
MenuConverter
menuConverter
;
private
final
PermissionService
permission
Service
;
private
final
SysRoleMenuService
roleMenu
Service
;
/**
...
...
@@ -224,7 +224,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
if
(
result
)
{
// 编辑刷新角色权限缓存
if
(
menuForm
.
getId
()
!=
null
)
{
permission
Service
.
refreshRolePermsCache
();
roleMenu
Service
.
refreshRolePermsCache
();
}
}
return
result
;
...
...
@@ -251,7 +251,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
*
* @param menuId 菜单ID
* @param visible 是否显示(1->显示;2->隐藏)
* @return
* @return
是否修改成功
*/
@Override
@CacheEvict
(
cacheNames
=
"menu"
,
key
=
"'routes'"
)
...
...
@@ -302,7 +302,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
// 刷新角色权限缓存
if
(
result
)
{
permission
Service
.
refreshRolePermsCache
();
roleMenu
Service
.
refreshRolePermsCache
();
}
return
result
;
...
...
src/main/java/com/youlai/system/service/impl/SysRoleMenuServiceImpl.java
浏览文件 @
93720b52
package
com.youlai.system.service.impl
;
import
cn.hutool.core.collection.CollectionUtil
;
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.model.bo.RolePermsBO
;
import
com.youlai.system.model.entity.SysRoleMenu
;
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
java.util.List
;
import
java.util.Set
;
/**
...
...
@@ -17,8 +23,79 @@ import java.util.List;
* @since 2.5.0
*/
@Service
@RequiredArgsConstructor
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集合
*
...
...
@@ -31,15 +108,4 @@ public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRo
return
menuIds
;
}
/**
* 获取权限角色列表
*
* @return 权限角色列表
*/
@Override
public
List
<
RolePermsBO
>
getRolePermsList
(
String
roleCode
)
{
List
<
RolePermsBO
>
rolePerms
=
this
.
baseMapper
.
getRolePermsList
(
roleCode
);
return
rolePerms
;
}
}
src/main/java/com/youlai/system/service/impl/SysUserRoleServiceImpl.java
浏览文件 @
93720b52
...
...
@@ -75,7 +75,7 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
* @return true:已分配 false:未分配
*/
@Override
public
boolean
isRoleAssignedToUser
(
Long
roleId
)
{
public
boolean
hasAssignedUsers
(
Long
roleId
)
{
int
count
=
this
.
baseMapper
.
countUsersForRole
(
roleId
);
return
count
>
0
;
}
...
...
src/main/resources/mapper/SysUserRoleMapper.xml
浏览文件 @
93720b52
...
...
@@ -14,13 +14,13 @@
user_id = #{userId}
</select>
<!--
统计角色下绑定的用户数量
-->
<!--
获取角色绑定的用户数
-->
<select
id=
"countUsersForRole"
resultType=
"java.lang.Integer"
>
SELECT
count(*)
FROM
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
AND t3.deleted = 0
WHERE
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录