SecurityUtils.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package com.zhongzheng.common.utils;
  2. import cn.hutool.http.HttpStatus;
  3. import com.zhongzheng.common.core.domain.model.TopLoginUser;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.security.core.Authentication;
  6. import org.springframework.security.core.context.SecurityContextHolder;
  7. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  8. import com.zhongzheng.common.core.domain.model.LoginUser;
  9. import com.zhongzheng.common.exception.CustomException;
  10. import org.springframework.stereotype.Component;
  11. import javax.annotation.PostConstruct;
  12. /**
  13. * 安全服务工具类
  14. *
  15. * @author zhongzheng
  16. */
  17. @Component
  18. public class SecurityUtils
  19. {
  20. // @Value("${mybatis-plus.tenant.enabled-tenant:true}")
  21. public static boolean EnabledTenant;
  22. @Value("${mybatis-plus.tenant.enabled-tenant:true}")
  23. private boolean enabledTenant;
  24. @PostConstruct
  25. public void getEnvironment(){
  26. EnabledTenant = this.enabledTenant;
  27. }
  28. /**
  29. * 获取用户账户
  30. **/
  31. public static String getUsername()
  32. {
  33. try
  34. {
  35. if(EnabledTenant){
  36. return getLoginUser().getUsername();
  37. }else{
  38. return getTopLoginUser().getUsername();
  39. }
  40. }
  41. catch (Exception e)
  42. {
  43. throw new CustomException("获取用户账户异常", HttpStatus.HTTP_UNAUTHORIZED);
  44. }
  45. }
  46. /**
  47. * 获取用户
  48. **/
  49. public static LoginUser getLoginUser()
  50. {
  51. try
  52. {
  53. return (LoginUser) getAuthentication().getPrincipal();
  54. }
  55. catch (Exception e)
  56. {
  57. throw new CustomException("获取用户信息异常", HttpStatus.HTTP_UNAUTHORIZED);
  58. }
  59. }
  60. public static TopLoginUser getTopLoginUser()
  61. {
  62. try
  63. {
  64. return (TopLoginUser) getAuthentication().getPrincipal();
  65. }
  66. catch (Exception e)
  67. {
  68. throw new CustomException("获取用户信息异常", HttpStatus.HTTP_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. }