SecurityUtils.java 3.6 KB

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