SecurityUtils.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.ruoyi.common.utils;
  2. import jdk.internal.util.Preconditions;
  3. import org.springframework.security.core.Authentication;
  4. import org.springframework.security.core.context.SecurityContextHolder;
  5. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  6. import com.ruoyi.common.constant.HttpStatus;
  7. import com.ruoyi.common.core.domain.model.LoginUser;
  8. import com.ruoyi.common.exception.ServiceException;
  9. /**
  10. * 安全服务工具类
  11. *
  12. * @author ruoyi
  13. */
  14. public class SecurityUtils
  15. {
  16. /**
  17. * 用户ID
  18. **/
  19. public static Long getUserId()
  20. {
  21. try
  22. {
  23. return getLoginUser().getUserId();
  24. }
  25. catch (Exception e)
  26. {
  27. throw new ServiceException("获取用户ID异常", HttpStatus.UNAUTHORIZED);
  28. }
  29. }
  30. /**
  31. * 获取部门ID
  32. **/
  33. public static Long getDeptId()
  34. {
  35. try
  36. {
  37. return getLoginUser().getDeptId();
  38. }
  39. catch (Exception e)
  40. {
  41. throw new ServiceException("获取部门ID异常", HttpStatus.UNAUTHORIZED);
  42. }
  43. }
  44. /**
  45. * 获取用户账户
  46. **/
  47. public static String getUsername()
  48. {
  49. try
  50. {
  51. return getLoginUser().getUsername();
  52. }
  53. catch (Exception e)
  54. {
  55. throw new ServiceException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
  56. }
  57. }
  58. /**
  59. * 获取用户
  60. **/
  61. public static LoginUser getLoginUser()
  62. {
  63. try
  64. {
  65. return (LoginUser) getAuthentication().getPrincipal();
  66. }
  67. catch (Exception e)
  68. {
  69. throw new ServiceException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
  70. }
  71. }
  72. /**
  73. * 获取Authentication
  74. */
  75. public static Authentication getAuthentication()
  76. {
  77. return SecurityContextHolder.getContext().getAuthentication();
  78. }
  79. /**
  80. * 生成BCryptPasswordEncoder密码
  81. *
  82. * @param password 密码
  83. * @return 加密字符串
  84. */
  85. public static String encryptPassword(String password)
  86. {
  87. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  88. return passwordEncoder.encode(password);
  89. }
  90. /**
  91. * 判断密码是否相同
  92. *
  93. * @param rawPassword 真实密码
  94. * @param encodedPassword 加密后字符
  95. * @return 结果
  96. */
  97. public static boolean matchesPassword(String rawPassword, String encodedPassword)
  98. {
  99. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  100. return passwordEncoder.matches(rawPassword, encodedPassword);
  101. }
  102. /**
  103. * 是否为管理员
  104. *
  105. * @param userId 用户ID
  106. * @return 结果
  107. */
  108. public static boolean isAdmin(Long userId)
  109. {
  110. return userId != null && 1L == userId;
  111. }
  112. /**
  113. * @brief 检测密码复杂度是否为 强
  114. * @param[in] password 密码字符串
  115. * @return 符合长度要求 返回true
  116. */
  117. public static String checkStrongPwd(String pwd) {
  118. try {
  119. if (!PwdCheckUtil.checkPasswordLength(pwd, "8", null)
  120. || !PwdCheckUtil.checkContainLowerCase(pwd)
  121. || !PwdCheckUtil.checkContainUpperCase(pwd)
  122. || !PwdCheckUtil.checkContainDigit(pwd)
  123. || !PwdCheckUtil.checkContainSpecialChar(pwd)
  124. ) {
  125. return "1";
  126. }
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. }
  130. return "0";
  131. }
  132. }