SecurityUtils.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.ruoyi.common.utils;
  2. import com.ruoyi.common.core.domain.entity.SysRole;
  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. import java.util.List;
  10. import java.util.concurrent.atomic.AtomicBoolean;
  11. /**
  12. * 安全服务工具类
  13. *
  14. * @author ruoyi
  15. */
  16. public class SecurityUtils
  17. {
  18. /**
  19. * 用户ID
  20. **/
  21. public static Long getUserId()
  22. {
  23. try
  24. {
  25. return getLoginUser().getUserId();
  26. }
  27. catch (Exception e)
  28. {
  29. throw new ServiceException("获取用户ID异常", HttpStatus.UNAUTHORIZED);
  30. }
  31. }
  32. /**
  33. * 获取部门ID
  34. **/
  35. public static Long getDeptId()
  36. {
  37. try
  38. {
  39. return getLoginUser().getDeptId();
  40. }
  41. catch (Exception e)
  42. {
  43. throw new ServiceException("获取部门ID异常", HttpStatus.UNAUTHORIZED);
  44. }
  45. }
  46. /**
  47. * 获取用户账户
  48. **/
  49. public static String getUsername()
  50. {
  51. try
  52. {
  53. return getLoginUser().getUsername();
  54. }
  55. catch (Exception e)
  56. {
  57. throw new ServiceException("获取用户账户异常", HttpStatus.UNAUTHORIZED);
  58. }
  59. }
  60. /**
  61. * 获取用户
  62. **/
  63. public static LoginUser getLoginUser()
  64. {
  65. try
  66. {
  67. return (LoginUser) getAuthentication().getPrincipal();
  68. }
  69. catch (Exception e)
  70. {
  71. throw new ServiceException("获取用户信息异常", HttpStatus.UNAUTHORIZED);
  72. }
  73. }
  74. public static List<SysRole> getUserRoles()
  75. {
  76. try
  77. {
  78. LoginUser u = (LoginUser) getAuthentication().getPrincipal();
  79. return u.getUser().getRoles();
  80. }
  81. catch (Exception e)
  82. {
  83. throw new ServiceException("获取用户权限异常", HttpStatus.UNAUTHORIZED);
  84. }
  85. }
  86. public static boolean hashRoles(Long roleId){
  87. List<SysRole> userRoles = getUserRoles();
  88. AtomicBoolean flag= new AtomicBoolean(false);
  89. userRoles.stream().forEach(e ->{
  90. if (e.getRoleId()==roleId){
  91. flag.set(true);
  92. }
  93. });
  94. return flag.get();
  95. }
  96. /**
  97. * 获取Authentication
  98. */
  99. public static Authentication getAuthentication()
  100. {
  101. return SecurityContextHolder.getContext().getAuthentication();
  102. }
  103. /**
  104. * 生成BCryptPasswordEncoder密码
  105. *
  106. * @param password 密码
  107. * @return 加密字符串
  108. */
  109. public static String encryptPassword(String password)
  110. {
  111. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  112. return passwordEncoder.encode(password);
  113. }
  114. /**
  115. * 判断密码是否相同
  116. *
  117. * @param rawPassword 真实密码
  118. * @param encodedPassword 加密后字符
  119. * @return 结果
  120. */
  121. public static boolean matchesPassword(String rawPassword, String encodedPassword)
  122. {
  123. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  124. return passwordEncoder.matches(rawPassword, encodedPassword);
  125. }
  126. /**
  127. * 是否为管理员
  128. *
  129. * @param userId 用户ID
  130. * @return 结果
  131. */
  132. public static boolean isAdmin(Long userId)
  133. {
  134. return userId != null && 1L == userId;
  135. }
  136. }