SecurityUtils.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.ruoyi.common.utils;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import com.ruoyi.common.constant.HttpStatus;
  4. import com.ruoyi.common.core.domain.entity.SysUser;
  5. import com.ruoyi.common.core.domain.model.LoginUser;
  6. import com.ruoyi.common.exception.ServiceException;
  7. import org.springframework.security.core.Authentication;
  8. import org.springframework.security.core.context.SecurityContextHolder;
  9. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.List;
  13. import java.util.stream.Collectors;
  14. /**
  15. * 安全服务工具类
  16. *
  17. * @author ruoyi
  18. */
  19. public class SecurityUtils {
  20. private static final ThreadLocal<String> HOLDER_TENANT_ID = new ThreadLocal<>();
  21. /**
  22. * 用户ID
  23. **/
  24. public static Long getUserId() {
  25. try {
  26. return getLoginUser().getUserId();
  27. } catch (Exception e) {
  28. throw new ServiceException("获取用户ID异常", HttpStatus.UNAUTHORIZED);
  29. }
  30. }
  31. /**
  32. * 获取公司ID
  33. **/
  34. public static Long getCompanyId() {
  35. try {
  36. return getLoginUser().getUser().getCompanyId();
  37. } catch (Exception e) {
  38. throw new ServiceException("获取用户归属公司ID异常", HttpStatus.UNAUTHORIZED);
  39. }
  40. }
  41. /**
  42. * 获取关联公司列表
  43. **/
  44. public static List<Long> getCompanyIds() {
  45. return Arrays.asList(getCompanyId());
  46. }
  47. public static List<Long> getUserCompanyIds() {
  48. try {
  49. SysUser user = getLoginUser().getUser();
  50. List<Long> companySetArr;
  51. //处理关联归属公司
  52. String companySet = user.getCompanySet();
  53. if (ObjectUtil.isEmpty(companySet)) {
  54. companySetArr = new ArrayList<>();
  55. } else {
  56. companySetArr = Arrays.asList(companySet.split(","))
  57. .stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
  58. }
  59. companySetArr.add(user.getCompanyId());//添加当前用户公司
  60. return companySetArr;
  61. } catch (Exception e) {
  62. throw new ServiceException("获取用户关联公司列表异常");
  63. }
  64. }
  65. /**
  66. * 获取部门ID
  67. **/
  68. public static Long getDeptId() {
  69. try {
  70. return getLoginUser().getDeptId();
  71. } catch (Exception e) {
  72. throw new ServiceException("获取部门ID异常", HttpStatus.UNAUTHORIZED);
  73. }
  74. }
  75. /**
  76. * 获取用户账户
  77. **/
  78. public static String getUsername() {
  79. try {
  80. return getLoginUser().getUsername();
  81. } catch (Exception e) {
  82. throw new ServiceException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
  83. }
  84. }
  85. /**
  86. * 获取租户id
  87. **/
  88. public static String getTenantId() {
  89. try {
  90. String tenantId = HOLDER_TENANT_ID.get();
  91. if (ObjectUtil.isNotEmpty(tenantId)) {
  92. return tenantId;
  93. }
  94. return getLoginUser().getUser().getTenantId();
  95. } catch (Exception e) {
  96. throw new ServiceException("获取租户id异常", HttpStatus.UNAUTHORIZED);
  97. }
  98. }
  99. /**
  100. * 临时设置租户Id(只在当前线程有效)
  101. */
  102. public static void setTenantId(String tenantId) {
  103. HOLDER_TENANT_ID.set(tenantId);
  104. }
  105. /**
  106. * 清除临时设置的租户Id
  107. */
  108. public static void clearTenantId() {
  109. HOLDER_TENANT_ID.remove();
  110. }
  111. /**
  112. * 获取用户
  113. **/
  114. public static LoginUser getLoginUser() {
  115. try {
  116. return (LoginUser) getAuthentication().getPrincipal();
  117. } catch (Exception e) {
  118. throw new ServiceException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
  119. }
  120. }
  121. /**
  122. * 获取Authentication
  123. */
  124. public static Authentication getAuthentication() {
  125. return SecurityContextHolder.getContext().getAuthentication();
  126. }
  127. /**
  128. * 生成BCryptPasswordEncoder密码
  129. *
  130. * @param password 密码
  131. * @return 加密字符串
  132. */
  133. public static String encryptPassword(String password) {
  134. // //密码强度校验
  135. // Pattern p = Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?!.*([a-zA-Z0-9])\\1{2}).{8,16}$");
  136. // Matcher m = p.matcher(password);
  137. // if(!m.find()){
  138. // throw new ServiceException("密码需要包含 非连续的 大写英文字母 小写英文字母 数字");
  139. // }
  140. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  141. return passwordEncoder.encode(password);
  142. }
  143. /**
  144. * 判断密码是否相同
  145. *
  146. * @param rawPassword 真实密码
  147. * @param encodedPassword 加密后字符
  148. * @return 结果
  149. */
  150. public static boolean matchesPassword(String rawPassword, String encodedPassword) {
  151. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  152. return passwordEncoder.matches(rawPassword, encodedPassword);
  153. }
  154. /**
  155. * 是否为管理员
  156. *
  157. * @param userId 用户ID
  158. * @return 结果
  159. */
  160. public static boolean isAdmin(Long userId) {
  161. return userId != null && 1L == userId;
  162. }
  163. }