SecurityUtils.java 3.9 KB

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