SecurityUtils.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package com.ruoyi.common.utils;
  2. import java.util.Collection;
  3. import java.util.List;
  4. import java.util.stream.Collectors;
  5. import org.springframework.security.core.Authentication;
  6. import org.springframework.security.core.context.SecurityContextHolder;
  7. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  8. import org.springframework.util.PatternMatchUtils;
  9. import com.ruoyi.common.constant.Constants;
  10. import com.ruoyi.common.constant.HttpStatus;
  11. import com.ruoyi.common.core.domain.entity.SysRole;
  12. import com.ruoyi.common.core.domain.model.LoginUser;
  13. import com.ruoyi.common.exception.ServiceException;
  14. /**
  15. * 安全服务工具类
  16. *
  17. * @author ruoyi
  18. */
  19. public class SecurityUtils
  20. {
  21. /**
  22. * 用户ID
  23. **/
  24. public static Long getUserId()
  25. {
  26. try
  27. {
  28. return getLoginUser().getUserId();
  29. }
  30. catch (Exception e)
  31. {
  32. throw new ServiceException("获取用户ID异常", HttpStatus.UNAUTHORIZED);
  33. }
  34. }
  35. /**
  36. * 获取部门ID
  37. **/
  38. public static Long getDeptId()
  39. {
  40. try
  41. {
  42. return getLoginUser().getDeptId();
  43. }
  44. catch (Exception e)
  45. {
  46. throw new ServiceException("获取部门ID异常", HttpStatus.UNAUTHORIZED);
  47. }
  48. }
  49. /**
  50. * 获取用户账户
  51. **/
  52. public static String getUsername()
  53. {
  54. try
  55. {
  56. return getLoginUser().getUsername();
  57. }
  58. catch (Exception e)
  59. {
  60. throw new ServiceException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
  61. }
  62. }
  63. /**
  64. * 获取用户
  65. **/
  66. public static LoginUser getLoginUser()
  67. {
  68. try
  69. {
  70. return (LoginUser) getAuthentication().getPrincipal();
  71. }
  72. catch (Exception e)
  73. {
  74. throw new ServiceException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
  75. }
  76. }
  77. /**
  78. * 获取Authentication
  79. */
  80. public static Authentication getAuthentication()
  81. {
  82. return SecurityContextHolder.getContext().getAuthentication();
  83. }
  84. /**
  85. * 生成BCryptPasswordEncoder密码
  86. *
  87. * @param password 密码
  88. * @return 加密字符串
  89. */
  90. public static String encryptPassword(String password)
  91. {
  92. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  93. return passwordEncoder.encode(password);
  94. }
  95. /**
  96. * 判断密码是否相同
  97. *
  98. * @param rawPassword 真实密码
  99. * @param encodedPassword 加密后字符
  100. * @return 结果
  101. */
  102. public static boolean matchesPassword(String rawPassword, String encodedPassword)
  103. {
  104. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  105. return passwordEncoder.matches(rawPassword, encodedPassword);
  106. }
  107. /**
  108. * 是否为管理员
  109. *
  110. * @param userId 用户ID
  111. * @return 结果
  112. */
  113. public static boolean isAdmin(Long userId)
  114. {
  115. return userId != null && 1L == userId;
  116. }
  117. /**
  118. * 验证用户是否具备某权限
  119. *
  120. * @param permission 权限字符串
  121. * @return 用户是否具备某权限
  122. */
  123. public static boolean hasPermi(String permission)
  124. {
  125. return hasPermi(getLoginUser().getPermissions(), permission);
  126. }
  127. /**
  128. * 判断是否包含权限
  129. *
  130. * @param authorities 权限列表
  131. * @param permission 权限字符串
  132. * @return 用户是否具备某权限
  133. */
  134. public static boolean hasPermi(Collection<String> authorities, String permission)
  135. {
  136. return authorities.stream().filter(StringUtils::hasText)
  137. .anyMatch(x -> Constants.ALL_PERMISSION.equals(x) || PatternMatchUtils.simpleMatch(x, permission));
  138. }
  139. /**
  140. * 验证用户是否拥有某个角色
  141. *
  142. * @param role 角色标识
  143. * @return 用户是否具备某角色
  144. */
  145. public static boolean hasRole(String role)
  146. {
  147. List<SysRole> roleList = getLoginUser().getUser().getRoles();
  148. Collection<String> roles = roleList.stream().map(SysRole::getRoleKey).collect(Collectors.toSet());
  149. return hasRole(roles, role);
  150. }
  151. /**
  152. * 判断是否包含角色
  153. *
  154. * @param roles 角色列表
  155. * @param role 角色
  156. * @return 用户是否具备某角色权限
  157. */
  158. public static boolean hasRole(Collection<String> roles, String role)
  159. {
  160. return roles.stream().filter(StringUtils::hasText)
  161. .anyMatch(x -> Constants.SUPER_ADMIN.equals(x) || PatternMatchUtils.simpleMatch(x, role));
  162. }
  163. }