TokenUtil.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the dreamlu.net developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: Chill 庄骞 (smallchill@163.com)
  16. */
  17. package org.springblade.auth.utils;
  18. import lombok.SneakyThrows;
  19. import org.springblade.common.constant.TenantConstant;
  20. import org.springblade.core.launch.constant.TokenConstant;
  21. import org.springblade.core.tenant.BladeTenantProperties;
  22. import org.springblade.core.tool.constant.BladeConstant;
  23. import org.springblade.core.tool.jackson.JsonUtil;
  24. import org.springblade.core.tool.utils.*;
  25. import org.springblade.system.entity.Tenant;
  26. import org.springframework.security.authentication.BadCredentialsException;
  27. import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;
  28. import org.springframework.security.oauth2.common.exceptions.UserDeniedAuthorizationException;
  29. import java.util.Base64;
  30. import java.util.Calendar;
  31. import java.util.Date;
  32. /**
  33. * 认证工具类
  34. *
  35. * @author Chill
  36. */
  37. public class TokenUtil {
  38. public final static String AVATAR = TokenConstant.AVATAR;
  39. public final static String ACCOUNT = TokenConstant.ACCOUNT;
  40. public final static String USER_NAME = TokenConstant.USER_NAME;
  41. public final static String NICK_NAME = TokenConstant.NICK_NAME;
  42. public final static String REAL_NAME = TokenConstant.REAL_NAME;
  43. public final static String USER_ID = TokenConstant.USER_ID;
  44. public final static String DEPT_ID = TokenConstant.DEPT_ID;
  45. public final static String POST_ID = TokenConstant.POST_ID;
  46. public final static String ROLE_ID = TokenConstant.ROLE_ID;
  47. public final static String ROLE_NAME = TokenConstant.ROLE_NAME;
  48. public final static String TENANT_ID = TokenConstant.TENANT_ID;
  49. public final static String OAUTH_ID = TokenConstant.OAUTH_ID;
  50. public final static String CLIENT_ID = TokenConstant.CLIENT_ID;
  51. public final static String DETAIL = TokenConstant.DETAIL;
  52. public final static String LICENSE = TokenConstant.LICENSE;
  53. public final static String LICENSE_NAME = TokenConstant.LICENSE_NAME;
  54. public final static String DEPT_HEADER_KEY = "Dept-Id";
  55. public final static String ROLE_HEADER_KEY = "Role-Id";
  56. public final static String CAPTCHA_HEADER_KEY = "Captcha-Key";
  57. public final static String CAPTCHA_HEADER_CODE = "Captcha-Code";
  58. public final static String CAPTCHA_NOT_CORRECT = "验证码不正确";
  59. public final static String TENANT_HEADER_KEY = "Tenant-Id";
  60. public final static String TENANT_PARAM_KEY = "tenant_id";
  61. public final static String DEFAULT_TENANT_ID = "000000";
  62. public final static String TENANT_NOT_FOUND = "租户ID未找到";
  63. public final static String USER_TYPE_HEADER_KEY = "User-Type";
  64. public final static String DEFAULT_USER_TYPE = "web";
  65. public final static String TOKEN_NOT_PERMISSION = "令牌授权已过期";
  66. public final static String USER_NOT_FOUND = "用户名或密码错误";
  67. public final static String USER_HAS_NO_ROLE = "未获得用户的角色信息";
  68. public final static String USER_HAS_NO_TENANT = "未获得用户的租户信息";
  69. public final static String USER_HAS_NO_TENANT_PERMISSION = "租户授权已过期,请联系管理员";
  70. public final static String USER_HAS_TOO_MANY_FAILS = "登录错误次数过多,请稍后再试";
  71. public final static String HEADER_KEY = "Authorization";
  72. public final static String HEADER_PREFIX = "Basic ";
  73. public final static String DEFAULT_AVATAR = "";
  74. public final static String PASSWORD_KEY = "password";
  75. public final static String GRANT_TYPE_KEY = "grant_type";
  76. public final static String REFRESH_TOKEN_KEY = "refresh_token";
  77. public final static String USER_STATUS_BAN = "该用户账号被封禁,请联系管理员";
  78. public final static String USER_ACCOUNT_NO_PERMISSION = "该用户账号没有对应权限,请联系管理员";
  79. public final static String USER_ACCOUNT_NO_TYPE = "该账号的用户类型分配异常,请联系管理员";
  80. private static BladeTenantProperties tenantProperties;
  81. /**
  82. * 获取租户配置
  83. *
  84. * @return tenantProperties
  85. */
  86. private static BladeTenantProperties getTenantProperties() {
  87. if (tenantProperties == null) {
  88. tenantProperties = SpringUtil.getBean(BladeTenantProperties.class);
  89. }
  90. return tenantProperties;
  91. }
  92. /**
  93. * 解码
  94. */
  95. @SneakyThrows
  96. public static String[] extractAndDecodeHeader() {
  97. String header = WebUtil.getRequest().getHeader(TokenUtil.HEADER_KEY);
  98. if (header == null || !header.startsWith(TokenUtil.HEADER_PREFIX)) {
  99. throw new UnapprovedClientAuthenticationException("请求头中无client信息");
  100. }
  101. byte[] base64Token = header.substring(6).getBytes(Charsets.UTF_8_NAME);
  102. byte[] decoded;
  103. try {
  104. decoded = Base64.getDecoder().decode(base64Token);
  105. } catch (IllegalArgumentException var7) {
  106. throw new BadCredentialsException("Failed to decode basic authentication token");
  107. }
  108. String token = new String(decoded, Charsets.UTF_8_NAME);
  109. int index = token.indexOf(StringPool.COLON);
  110. if (index == -1) {
  111. throw new BadCredentialsException("Invalid basic authentication token");
  112. } else {
  113. return new String[]{token.substring(0, index), token.substring(index + 1)};
  114. }
  115. }
  116. /**
  117. * 获取请求头中的客户端id
  118. */
  119. public static String getClientIdFromHeader() {
  120. String[] tokens = extractAndDecodeHeader();
  121. return tokens[0];
  122. }
  123. /**
  124. * 获取token过期时间(次日凌晨3点)
  125. *
  126. * @return expire
  127. */
  128. public static int getTokenValiditySecond() {
  129. Calendar cal = Calendar.getInstance();
  130. cal.add(Calendar.DAY_OF_YEAR, 1);
  131. cal.set(Calendar.HOUR_OF_DAY, 3);
  132. cal.set(Calendar.SECOND, 0);
  133. cal.set(Calendar.MINUTE, 0);
  134. cal.set(Calendar.MILLISECOND, 0);
  135. return (int) (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
  136. }
  137. /**
  138. * 获取refreshToken过期时间
  139. *
  140. * @return expire
  141. */
  142. public static int getRefreshTokenValiditySeconds() {
  143. return 60 * 60 * 24 * 15;
  144. }
  145. /**
  146. * 判断租户权限
  147. *
  148. * @param tenant 租户信息
  149. * @return boolean
  150. */
  151. public static boolean judgeTenant(Tenant tenant) {
  152. if (tenant == null || tenant.getId() == null) {
  153. throw new UserDeniedAuthorizationException(TokenUtil.USER_HAS_NO_TENANT);
  154. }
  155. if (StringUtil.equalsIgnoreCase(tenant.getTenantId(), BladeConstant.ADMIN_TENANT_ID)) {
  156. return false;
  157. }
  158. Date expireTime = tenant.getExpireTime();
  159. if (getTenantProperties().getLicense()) {
  160. String licenseKey = tenant.getLicenseKey();
  161. String decrypt = DesUtil.decryptFormHex(licenseKey, TenantConstant.DES_KEY);
  162. expireTime = JsonUtil.parse(decrypt, Tenant.class).getExpireTime();
  163. }
  164. if (expireTime != null && expireTime.before(DateUtil.now())) {
  165. throw new UserDeniedAuthorizationException(TokenUtil.USER_HAS_NO_TENANT_PERMISSION);
  166. }
  167. return false;
  168. }
  169. }