SecurityUtils.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.zhongzheng.common.util;
  2. import cn.hutool.http.HttpStatus;
  3. import com.zhongzheng.common.croe.LoginUser;
  4. import com.zhongzheng.common.filter.CustomException;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.security.core.Authentication;
  7. import org.springframework.security.core.context.SecurityContextHolder;
  8. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  9. import org.springframework.stereotype.Component;
  10. import javax.annotation.PostConstruct;
  11. /**
  12. * 安全服务工具类
  13. *
  14. * @author zhongzheng
  15. */
  16. @Component
  17. public class SecurityUtils
  18. {
  19. // @Value("${mybatis-plus.tenant.enabled-tenant:true}")
  20. public static boolean EnabledTenant;
  21. @Value("${mybatis-plus.tenant.enabled-tenant:true}")
  22. private boolean enabledTenant;
  23. @PostConstruct
  24. public void getEnvironment(){
  25. EnabledTenant = this.enabledTenant;
  26. }
  27. /**
  28. * 获取用户账户
  29. **/
  30. public static String getUsername()
  31. {
  32. try
  33. {
  34. return getLoginUser().getUsername();
  35. }
  36. catch (Exception e)
  37. {
  38. throw new CustomException("获取用户账户异常", HttpStatus.HTTP_UNAUTHORIZED);
  39. }
  40. }
  41. /**
  42. * 获取用户
  43. **/
  44. public static LoginUser getLoginUser()
  45. {
  46. try
  47. {
  48. return (LoginUser) getAuthentication().getPrincipal();
  49. }
  50. catch (Exception e)
  51. {
  52. throw new CustomException("获取用户信息异常", HttpStatus.HTTP_UNAUTHORIZED);
  53. }
  54. }
  55. /**
  56. * 获取Authentication
  57. */
  58. public static Authentication getAuthentication()
  59. {
  60. return SecurityContextHolder.getContext().getAuthentication();
  61. }
  62. /**
  63. * 生成BCryptPasswordEncoder密码
  64. *
  65. * @param password 密码
  66. * @return 加密字符串
  67. */
  68. public static String encryptPassword(String password)
  69. {
  70. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  71. return passwordEncoder.encode(password);
  72. }
  73. /**
  74. * 判断密码是否相同
  75. *
  76. * @param rawPassword 真实密码
  77. * @param encodedPassword 加密后字符
  78. * @return 结果
  79. */
  80. public static boolean matchesPassword(String rawPassword, String encodedPassword)
  81. {
  82. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
  83. return passwordEncoder.matches(rawPassword, encodedPassword);
  84. }
  85. /**
  86. * 是否为管理员
  87. *
  88. * @param userId 用户ID
  89. * @return 结果
  90. */
  91. public static boolean isAdmin(Long userId)
  92. {
  93. LoginUser user= (LoginUser) getAuthentication().getPrincipal();
  94. return user != null && "admin".equals(user.getUser().getUserName());
  95. // return userId != null && 1L == userId;
  96. }
  97. }