PermissionService.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.zsElectric.boot.security.service;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.zsElectric.boot.common.constant.RedisConstants;
  5. import com.zsElectric.boot.security.util.SecurityUtils;
  6. import lombok.RequiredArgsConstructor;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.data.redis.core.RedisTemplate;
  9. import org.springframework.stereotype.Component;
  10. import org.springframework.util.PatternMatchUtils;
  11. import java.util.*;
  12. /**
  13. * SpringSecurity 权限校验
  14. *
  15. * @author haoxr
  16. * @since 2022/2/22
  17. */
  18. @Component("ss")
  19. @RequiredArgsConstructor
  20. @Slf4j
  21. public class PermissionService {
  22. private final RedisTemplate<String, Object> redisTemplate;
  23. /**
  24. * 判断当前登录用户是否拥有操作权限
  25. *
  26. * @param requiredPerm 所需权限
  27. * @return 是否有权限
  28. */
  29. public boolean hasPerm(String requiredPerm) {
  30. log.info("========== 权限校验开始 ==========");
  31. log.info("所需权限: {}", requiredPerm);
  32. if (StrUtil.isBlank(requiredPerm)) {
  33. log.warn("权限标识为空,拒绝访问");
  34. return false;
  35. }
  36. // 超级管理员放行
  37. if (SecurityUtils.isRoot()) {
  38. log.info("超级管理员,直接放行");
  39. return true;
  40. }
  41. // 获取当前登录用户的角色编码集合
  42. Set<String> roleCodes = SecurityUtils.getRoles();
  43. log.info("当前用户角色: {}", roleCodes);
  44. if (CollectionUtil.isEmpty(roleCodes)) {
  45. log.warn("用户角色为空,拒绝访问");
  46. return false;
  47. }
  48. // 获取当前登录用户的所有角色的权限列表
  49. Set<String> rolePerms = this.getRolePermsFormCache(roleCodes);
  50. log.info("角色拥有的权限列表: {}", rolePerms);
  51. if (CollectionUtil.isEmpty(rolePerms)) {
  52. log.warn("角色权限为空(Redis缓存中无数据),拒绝访问");
  53. return false;
  54. }
  55. // 判断当前登录用户的所有角色的权限列表中是否包含所需权限
  56. boolean hasPermission = rolePerms.stream()
  57. .anyMatch(rolePerm ->
  58. // 匹配权限,支持通配符(* 等)
  59. PatternMatchUtils.simpleMatch(rolePerm, requiredPerm)
  60. );
  61. if (!hasPermission) {
  62. log.error("用户无操作权限:{}",requiredPerm);
  63. }
  64. return hasPermission;
  65. }
  66. /**
  67. * 从缓存中获取角色权限列表
  68. *
  69. * @param roleCodes 角色编码集合
  70. * @return 角色权限列表
  71. */
  72. public Set<String> getRolePermsFormCache(Set<String> roleCodes) {
  73. // 检查输入是否为空
  74. if (CollectionUtil.isEmpty(roleCodes)) {
  75. return Collections.emptySet();
  76. }
  77. Set<String> perms = new HashSet<>();
  78. // 从缓存中一次性获取所有角色的权限
  79. Collection<Object> roleCodesAsObjects = new ArrayList<>(roleCodes);
  80. List<Object> rolePermsList = redisTemplate.opsForHash().multiGet(RedisConstants.System.ROLE_PERMS, roleCodesAsObjects);
  81. for (Object rolePermsObj : rolePermsList) {
  82. if (rolePermsObj instanceof Set) {
  83. @SuppressWarnings("unchecked")
  84. Set<String> rolePerms = (Set<String>) rolePermsObj;
  85. perms.addAll(rolePerms);
  86. }
  87. }
  88. return perms;
  89. }
  90. }