|
@@ -2,7 +2,9 @@ package com.hfln.portal.infrastructure.config;
|
|
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
import cn.hfln.framework.redis.util.RedisUtil;
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
import com.hfln.portal.common.constant.redis.RedisCacheConstant;
|
|
|
+import com.hfln.portal.common.util.SaTokenUtil;
|
|
|
import com.hfln.portal.infrastructure.po.TblPermission;
|
|
|
import com.hfln.portal.infrastructure.po.TblRole;
|
|
|
import com.hfln.portal.infrastructure.po.TblRolePermission;
|
|
@@ -16,7 +18,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
-import java.util.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -47,21 +51,21 @@ public class UserAuthService {
|
|
|
|
|
|
public void cacheRolesAndPermissions() {
|
|
|
|
|
|
- Set<String> roleCodeSet = this.getRoleCodeSet(StpUtil.getLoginId(), StpUtil.getLoginType());
|
|
|
- if (!CollectionUtils.isEmpty(roleCodeSet)) {
|
|
|
+ List<String> roleCodeList = this.getRoleCodeList(StpUtil.getLoginId(), StpUtil.getLoginType());
|
|
|
+ if (!CollectionUtils.isEmpty(roleCodeList)) {
|
|
|
// 登录缓存角色 防止用户角色有变更,登录前先删除之前的缓存
|
|
|
if (!redisService.hasKey(RedisCacheConstant.USER_ROLE_KEY_PRE+StpUtil.getLoginId())) {
|
|
|
// 这里从更改用户角色的地方修改最好
|
|
|
-// redisService.deleteObject(RedisCacheConstant.USER_ROLE_KEY_PRE+StpUtil.getLoginId());
|
|
|
- redisService.sAdd(RedisCacheConstant.USER_ROLE_KEY_PRE+ StpUtil.getLoginId(), roleCodeSet.toArray());
|
|
|
+ redisService.del(RedisCacheConstant.USER_ROLE_KEY_PRE+StpUtil.getLoginId());
|
|
|
+ redisService.set(RedisCacheConstant.USER_ROLE_KEY_PRE+ StpUtil.getLoginId(), JSONArray.toJSONString(roleCodeList));
|
|
|
}
|
|
|
- for (String roleCode : roleCodeSet) {
|
|
|
+ for (String roleCode : roleCodeList) {
|
|
|
if (!redisService.hasKey(RedisCacheConstant.ROLE_PERM_KEY_PRE+roleCode)) {
|
|
|
// 这里从更改角色权限的地方修改最好
|
|
|
// redisService.deleteObject(RedisCacheConstant.ROLE_PERM_KEY_PRE+roleCode);
|
|
|
- Set<String> permCodeSet = this.getPermCodeSet(roleCode);
|
|
|
- if (!CollectionUtils.isEmpty(permCodeSet)) {
|
|
|
- redisService.sAdd(RedisCacheConstant.ROLE_PERM_KEY_PRE+roleCode, permCodeSet.toArray());
|
|
|
+ List<String> permCodeList = this.getPermCodeList(roleCode);
|
|
|
+ if (!CollectionUtils.isEmpty(permCodeList)) {
|
|
|
+ redisService.set(RedisCacheConstant.ROLE_PERM_KEY_PRE+roleCode, JSONArray.toJSONString(permCodeList));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -69,96 +73,100 @@ public class UserAuthService {
|
|
|
// 每次修改用户角色,应该注销用户登录信息,让用户重新登录
|
|
|
}
|
|
|
|
|
|
+ public void delUserRoleCache() {
|
|
|
+ redisService.del(RedisCacheConstant.USER_ROLE_KEY_PRE+StpUtil.getLoginId());
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 返回一个账号所拥有的权限码集合
|
|
|
*/
|
|
|
- public Set<String> getPermCodeSet(Object loginIdObj, String loginType) {
|
|
|
+ public List<String> getPermCodeList(Object loginIdObj, String loginType) {
|
|
|
|
|
|
- Set<String> set = new HashSet<>();
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
// 后期接口调用次数大的话,可以放redis ,再加更新操作
|
|
|
long loginId = Long.parseLong(String.valueOf(loginIdObj));
|
|
|
|
|
|
List<TblUserRole> userRoles = userRoleService.findByUserId((long)loginId);
|
|
|
if (CollectionUtils.isEmpty(userRoles)) {
|
|
|
log.info("当前用户没有配置角色,userId:{}", loginId);
|
|
|
- return set;
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
List<Long> userIds = userRoles.stream().map(TblUserRole::getRoleId).collect(Collectors.toList());
|
|
|
List<TblRole> roles = roleService.findAvailableByIds(userIds);
|
|
|
if (CollectionUtils.isEmpty(roles)) {
|
|
|
log.info("角色配置异常, userIds :{}", roles);
|
|
|
- return set;
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
List<Long> roleIds = roles.stream().map(TblRole::getRoleId).collect(Collectors.toList());
|
|
|
List<TblRolePermission> rolePermissions = rolePermissionService.findByRoleIds(roleIds);
|
|
|
if (CollectionUtils.isEmpty(rolePermissions)) {
|
|
|
log.info("当前角色没有配置权限,roleIds:{}", roleIds);
|
|
|
- return set;
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
List<Long> permIds = rolePermissions.stream().map(TblRolePermission::getPermId).collect(Collectors.toList());
|
|
|
List<TblPermission> permissionList = permissionService.findAvailableByIds(permIds);
|
|
|
if (CollectionUtils.isEmpty(permissionList)) {
|
|
|
log.info("权限配置异常, permIds :{}", permIds);
|
|
|
- return set;
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
- set.addAll(permissionList.stream().map(TblPermission::getPermCode).collect(Collectors.toSet()));
|
|
|
- return set;
|
|
|
+ list.addAll(permissionList.stream().map(TblPermission::getPermCode).collect(Collectors.toList()));
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 返回一个账号所拥有的角色标识集合
|
|
|
*/
|
|
|
- public Set<String> getRoleCodeSet(Object loginIdObj, String loginType) {
|
|
|
+ public List<String> getRoleCodeList(Object loginIdObj, String loginType) {
|
|
|
|
|
|
- Set<String> set = new HashSet<>();
|
|
|
- long loginId = Long.parseLong(String.valueOf(loginIdObj));
|
|
|
+ List<String> roleCodeList = new ArrayList<>();
|
|
|
+ long loginId = SaTokenUtil.getUserId(loginIdObj);
|
|
|
List<TblUserRole> userRoles = userRoleService.findByUserId(loginId);
|
|
|
if (CollectionUtils.isEmpty(userRoles)) {
|
|
|
log.info("当前用户没有配置角色,userId:{}", loginId);
|
|
|
- return set;
|
|
|
+ return roleCodeList;
|
|
|
}
|
|
|
|
|
|
- List<Long> userIds = userRoles.stream().map(TblUserRole::getRoleId).collect(Collectors.toList());
|
|
|
- List<TblRole> roles = roleService.findAvailableByIds(userIds);
|
|
|
+ List<Long> roleIds = userRoles.stream().map(TblUserRole::getRoleId).collect(Collectors.toList());
|
|
|
+ List<TblRole> roles = roleService.findAvailableByIds(roleIds);
|
|
|
if (CollectionUtils.isEmpty(roles)) {
|
|
|
- log.info("角色配置异常, userIds :{}", roles);
|
|
|
- return set;
|
|
|
+ log.info("角色配置异常, roleIds :{}", roleIds);
|
|
|
+ return roleCodeList;
|
|
|
}
|
|
|
- set.addAll(roles.stream().map(TblRole::getRoleCode).collect(Collectors.toSet()));
|
|
|
- return set;
|
|
|
+ roleCodeList.addAll(roles.stream().map(TblRole::getRoleCode).collect(Collectors.toList()));
|
|
|
+ return roleCodeList;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 返回当前角色所拥有的权限码集合
|
|
|
*/
|
|
|
- public Set<String> getPermCodeSet(String roleCode) {
|
|
|
+ public List<String> getPermCodeList(String roleCode) {
|
|
|
|
|
|
- Set<String> set = new HashSet<>();
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
|
|
|
TblRole role = roleService.findAvailableByCode(roleCode);
|
|
|
if (role == null) {
|
|
|
log.info("角色配置异常, userIds :{}", role);
|
|
|
- return set;
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
List<TblRolePermission> rolePermissions = rolePermissionService.findByRoleIds(Arrays.asList(role.getRoleId()));
|
|
|
if (CollectionUtils.isEmpty(rolePermissions)) {
|
|
|
log.info("当前角色没有配置权限,roleCode:{}", roleCode);
|
|
|
- return set;
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
List<Long> permIds = rolePermissions.stream().map(TblRolePermission::getPermId).collect(Collectors.toList());
|
|
|
List<TblPermission> permissionList = permissionService.findAvailableByIds(permIds);
|
|
|
if (CollectionUtils.isEmpty(permissionList)) {
|
|
|
log.info("权限配置异常, permIds :{}", permIds);
|
|
|
- return set;
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
- set.addAll(permissionList.stream().map(TblPermission::getPermCode).collect(Collectors.toSet()));
|
|
|
- return set;
|
|
|
+ list.addAll(permissionList.stream().map(TblPermission::getPermCode).collect(Collectors.toSet()));
|
|
|
+ return list;
|
|
|
}
|
|
|
}
|