SecurityUtils.java 5.4 KB

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