|
@@ -1,105 +1,122 @@
|
|
-package com.backendsys.modules.common.config.security.utils;
|
|
|
|
-
|
|
|
|
-import com.backendsys.exception.CustException;
|
|
|
|
-import com.backendsys.modules.common.config.security.entity.SecurityUserInfo;
|
|
|
|
-import com.backendsys.utils.response.ResultEnum;
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
|
-import org.springframework.util.CollectionUtils;
|
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
|
-
|
|
|
|
-import java.util.*;
|
|
|
|
-
|
|
|
|
-/**
|
|
|
|
- * 自定义权限 @PreAuthorize("@ss.hasPermi('3.2.3') && @ss.isSuper()")
|
|
|
|
- */
|
|
|
|
-@Service("ss")
|
|
|
|
-public class PermissionUtil {
|
|
|
|
- //private static final String ALL_PERMISSION = "*:*:*";
|
|
|
|
- @Autowired
|
|
|
|
- private TokenUtil tokenUtil;
|
|
|
|
- /**
|
|
|
|
- * 验证用户是否具备权限
|
|
|
|
- * @param permi 权限字符串
|
|
|
|
- * @return boolean
|
|
|
|
- */
|
|
|
|
-// public boolean hasPermi(String permi) {
|
|
|
|
- public boolean hasPermi(Collection<String> permi) {
|
|
|
|
-
|
|
|
|
- if (CollectionUtils.isEmpty(permi)) return false;
|
|
|
|
-
|
|
|
|
- SecurityUserInfo securityUserInfo = SecurityUtil.getUserInfo();
|
|
|
|
- // 如果是超级管理员,则直接通过
|
|
|
|
- if (securityUserInfo.getIs_super() == 1) return true;
|
|
|
|
-
|
|
|
|
- // 没有 permission_ids 即不是系统用户,即没有访问后台的权限
|
|
|
|
- if (securityUserInfo != null) {
|
|
|
|
- List<String> permission_ids = securityUserInfo.getPermission_ids();
|
|
|
|
- if (permission_ids != null && !permission_ids.isEmpty()) {
|
|
|
|
- Set<String> dataSet = new HashSet<>();
|
|
|
|
- for (String permission_id : permission_ids) {
|
|
|
|
- dataSet.add(permission_id);
|
|
|
|
- }
|
|
|
|
- for (String permission : permi) {
|
|
|
|
- if (hasPermissions(dataSet, permission)) {
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- // return hasPermissions(modulesSet, permission);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- public boolean hasPermissions(Set<String> permi, String permission) {
|
|
|
|
- return permi.contains(StringUtils.trimAllWhitespace(permission));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 是否超级管理员
|
|
|
|
- public boolean isSuper() {
|
|
|
|
- SecurityUserInfo securityUserInfo = SecurityUtil.getUserInfo();
|
|
|
|
- return securityUserInfo.getIs_super() == 1;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 是否超级管理员 (首位)
|
|
|
|
- public boolean isFirstSuper() {
|
|
|
|
- SecurityUserInfo securityUserInfo = SecurityUtil.getUserInfo();
|
|
|
|
- return securityUserInfo.getUser_id() == 1;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 是否会员
|
|
|
|
- public boolean isMember() {
|
|
|
|
- SecurityUserInfo securityUserInfo = SecurityUtil.getUserInfo();
|
|
|
|
- return "Member".equals(securityUserInfo.getTarget());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 判断是否匹配当前 {用户ID} 与 {用户权限},不是则抛出错误
|
|
|
|
- * - 匹配,通过
|
|
|
|
- * - 不匹配,再次检查权限
|
|
|
|
- * - 匹配,通过
|
|
|
|
- * - 不匹配,抛出错误
|
|
|
|
- * permissionService.checkUserIdAndPermission(sysUserDTO.getUser_id(), "3.2.1");
|
|
|
|
- */
|
|
|
|
- public void checkUserIdAndPermission(long user_id, Collection<String> permis) {
|
|
|
|
- SecurityUserInfo securityUserInfo = SecurityUtil.getUserInfo();
|
|
|
|
- if (securityUserInfo.getUser_id() != user_id) {
|
|
|
|
- if (!hasPermi(permis)) {
|
|
|
|
- throw new CustException(ResultEnum.AUTH_ROLE_ERROR.getMessage(), ResultEnum.AUTH_ROLE_ERROR.getCode());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 判断是否 首位超级管理员 (id:1),不是则抛出错误
|
|
|
|
- */
|
|
|
|
- public void checkSuperAdminOfFirst(long user_id) {
|
|
|
|
- if (user_id == 1) {
|
|
|
|
- SecurityUserInfo securityUserInfo = SecurityUtil.getUserInfo();
|
|
|
|
- if (securityUserInfo.getUser_id() != 1) {
|
|
|
|
- throw new CustException(ResultEnum.AUTH_USER_ERROR.getMessage(), ResultEnum.AUTH_USER_ERROR.getCode());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-}
|
|
|
|
|
|
+//package com.backendsys.modules.common.config.security.utils;
|
|
|
|
+//
|
|
|
|
+//import com.backendsys.exception.CustException;
|
|
|
|
+//import com.backendsys.modules.common.config.redis.utils.RedisUtil;
|
|
|
|
+//import com.backendsys.modules.common.config.security.entity.SecurityUserInfo;
|
|
|
|
+//import com.backendsys.utils.response.ResultEnum;
|
|
|
|
+//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+//import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+//import org.springframework.stereotype.Service;
|
|
|
|
+//import org.springframework.util.CollectionUtils;
|
|
|
|
+//import org.springframework.util.StringUtils;
|
|
|
|
+//
|
|
|
|
+//import java.util.*;
|
|
|
|
+//
|
|
|
|
+///**
|
|
|
|
+// * 即将弃用
|
|
|
|
+// * 自定义权限 @PreAuthorize("@sr.hasPermission('3.2.3') && @ss.isSuper()")
|
|
|
|
+// */
|
|
|
|
+//@Service("ss")
|
|
|
|
+//public class PermissionUtil {
|
|
|
|
+// //private static final String ALL_PERMISSION = "*:*:*";
|
|
|
|
+//
|
|
|
|
+// @Value("${REDIS_LOGIN_TOKEN_PREFIX}")
|
|
|
|
+// private String REDIS_LOGIN_TOKEN_PREFIX;
|
|
|
|
+// @Value("${REDIS_LOGIN_PERMISSION_PREFIX}")
|
|
|
|
+// private String REDIS_LOGIN_PERMISSION_PREFIX;
|
|
|
|
+//
|
|
|
|
+// @Autowired
|
|
|
|
+// private RedisUtil redisUtil;
|
|
|
|
+//
|
|
|
|
+// /**
|
|
|
|
+// * 验证用户是否具备权限
|
|
|
|
+// * @param permi 权限字符串
|
|
|
|
+// * @return boolean
|
|
|
|
+// */
|
|
|
|
+//// public boolean hasPermi(String permi) {
|
|
|
|
+// public boolean hasPermi(Collection<String> permi) {
|
|
|
|
+//
|
|
|
|
+// if (CollectionUtils.isEmpty(permi)) return false;
|
|
|
|
+//
|
|
|
|
+// SecurityUserInfo securityUserInfo = SecurityUtil.getUserInfo();
|
|
|
|
+// // 如果是超级管理员,则直接通过
|
|
|
|
+// if (securityUserInfo.getIs_super() == 1) return true;
|
|
|
|
+//
|
|
|
|
+// // 没有 permission_ids 即不是系统用户,即没有访问后台的权限
|
|
|
|
+// if (securityUserInfo != null) {
|
|
|
|
+//
|
|
|
|
+//// String uuid = securityUserInfo.getLast_login_uuid();
|
|
|
|
+//// String redis_key = REDIS_LOGIN_PERMISSION_PREFIX + uuid;
|
|
|
|
+//// String permission_ids_str = redisUtil.getCacheObject(redis_key);
|
|
|
|
+//// List<String> permission_ids = Arrays.asList(permission_ids_str.split(","));
|
|
|
|
+//
|
|
|
|
+// List<String> permission_ids = securityUserInfo.getPermission_ids();
|
|
|
|
+//
|
|
|
|
+// if (permission_ids != null && !permission_ids.isEmpty()) {
|
|
|
|
+// Set<String> dataSet = new HashSet<>();
|
|
|
|
+// for (String permission_id : permission_ids) {
|
|
|
|
+// dataSet.add(permission_id);
|
|
|
|
+// }
|
|
|
|
+// for (String permission : permi) {
|
|
|
|
+// if (hasPermissions(dataSet, permission)) {
|
|
|
|
+// return true;
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// // return hasPermissions(modulesSet, permission);
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// return false;
|
|
|
|
+// }
|
|
|
|
+// public boolean hasPermissions(Set<String> permi, String permission) {
|
|
|
|
+// return permi.contains(StringUtils.trimAllWhitespace(permission));
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// // 是否超级管理员
|
|
|
|
+// public boolean isSuper() {
|
|
|
|
+// SecurityUserInfo securityUserInfo = SecurityUtil.getUserInfo();
|
|
|
|
+// return securityUserInfo.getIs_super() == 1;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// // 是否超级管理员 (首位)
|
|
|
|
+// public boolean isFirstSuper() {
|
|
|
|
+// SecurityUserInfo securityUserInfo = SecurityUtil.getUserInfo();
|
|
|
|
+// return securityUserInfo.getUser_id() == 1;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// // 是否会员
|
|
|
|
+// public boolean isMember() {
|
|
|
|
+// SecurityUserInfo securityUserInfo = SecurityUtil.getUserInfo();
|
|
|
|
+// return "Member".equals(securityUserInfo.getTarget());
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+//
|
|
|
|
+// /**
|
|
|
|
+// * 判断是否匹配当前 {用户ID} 与 {用户权限},不是则抛出错误
|
|
|
|
+// * - 匹配,通过
|
|
|
|
+// * - 不匹配,再次检查权限
|
|
|
|
+// * - 匹配,通过
|
|
|
|
+// * - 不匹配,抛出错误
|
|
|
|
+// * permissionService.checkUserIdAndPermission(sysUserDTO.getUser_id(), "3.2.1");
|
|
|
|
+// */
|
|
|
|
+// public void checkUserIdAndPermission(long user_id, Collection<String> permis) {
|
|
|
|
+// SecurityUserInfo securityUserInfo = SecurityUtil.getUserInfo();
|
|
|
|
+// if (securityUserInfo.getUser_id() != user_id) {
|
|
|
|
+// if (!hasPermi(permis)) {
|
|
|
|
+// throw new CustException(ResultEnum.AUTH_ROLE_ERROR.getMessage(), ResultEnum.AUTH_ROLE_ERROR.getCode());
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+// /**
|
|
|
|
+// * 判断是否 首位超级管理员 (id:1),不是则抛出错误
|
|
|
|
+// */
|
|
|
|
+// public void checkSuperAdminOfFirst(long user_id) {
|
|
|
|
+// if (user_id == 1) {
|
|
|
|
+// SecurityUserInfo securityUserInfo = SecurityUtil.getUserInfo();
|
|
|
|
+// if (securityUserInfo.getUser_id() != 1) {
|
|
|
|
+// throw new CustException(ResultEnum.AUTH_USER_ERROR.getMessage(), ResultEnum.AUTH_USER_ERROR.getCode());
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+//}
|