JwtUtil.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package com.dk.common.util.oauth;
  2. import com.auth0.jwt.JWT;
  3. import com.auth0.jwt.JWTVerifier;
  4. import com.auth0.jwt.algorithms.Algorithm;
  5. import com.auth0.jwt.exceptions.JWTDecodeException;
  6. import com.auth0.jwt.interfaces.DecodedJWT;
  7. import com.dk.common.infrastructure.config.ConfigStatic;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest;
  10. import javax.servlet.http.HttpServletRequest;
  11. @Slf4j
  12. public class JwtUtil {
  13. public static final String SHIRO_CP_ID = "ftyId";
  14. public static final String SHIRO_CP_CODE = "ftyCode";
  15. public static final String SHIRO_STAFF_NAME = "username";
  16. public static final String SHIRO_STAFF_ID = "userId";
  17. public static final String SHIRO_APP_CODE = "appCode";
  18. public static final String SHIRO_CLIENT_ID = "clientId";
  19. public static final String SHIRO_USER_SALT = "salt";
  20. public static final String SHIRO_ISSUER = "Issuer";
  21. public static final String SHIRO_SUBJECT = "long_token";
  22. public static final String SHIRO_GRANT_TYPE = "grantType";
  23. public static final String SHIRO_APP_LANG = "lang";
  24. /**
  25. * 校验token是否正确
  26. *
  27. * @param token 密钥
  28. * @param salt 盐值
  29. * @return 是否正确
  30. */
  31. public static boolean verify(String token, String salt) {
  32. try {
  33. Algorithm algorithm = Algorithm.HMAC256(salt);
  34. JWTVerifier verifier = JWT.require(algorithm)
  35. // 签发人
  36. .withIssuer(SHIRO_ISSUER)
  37. // 主题
  38. .withSubject(SHIRO_SUBJECT)
  39. // 签发的目标
  40. //.withAudience(jwtProperties.getAudience())
  41. .build();
  42. DecodedJWT jwt = verifier.verify(token);
  43. if (jwt != null) {
  44. return true;
  45. }
  46. } catch (Exception e) {
  47. log.error("The token is invalid{}", e.getMessage());
  48. }
  49. return false;
  50. }
  51. /**
  52. * 获取AES解密token
  53. *
  54. * @param request
  55. * @return
  56. */
  57. public static String getDecryptToken(HttpServletRequest request, String AESKey) {
  58. String token = "";
  59. try {
  60. // 构建 OAuth2 资源请求
  61. OAuthAccessResourceRequest oauthRequest = new OAuthAccessResourceRequest(request);
  62. // 获取Access Token
  63. String accessToken = oauthRequest.getAccessToken();
  64. token = AESSecurityUtil.decrypt(AESKey, accessToken);
  65. return token;
  66. } catch (Exception e) {
  67. throw new RuntimeException(e);
  68. }
  69. }
  70. /**
  71. * @desc : 通过已解密的token获取staffName
  72. * @author : 周兴
  73. * @date : 2023/2/26 16:07
  74. */
  75. public static String getStaffName(String token) {
  76. try {
  77. DecodedJWT jwt = JWT.decode(token);
  78. return jwt.getClaim(SHIRO_STAFF_NAME).asString();
  79. } catch (JWTDecodeException e) {
  80. log.error("error:{}", e.getMessage());
  81. return null;
  82. }
  83. }
  84. /**
  85. * @desc : 通过已解密的token获取staffId
  86. * @author : 周兴
  87. * @date : 2023/2/26 16:07
  88. */
  89. public static String getStaffId(String token) {
  90. try {
  91. DecodedJWT jwt = JWT.decode(token);
  92. return jwt.getClaim(SHIRO_STAFF_ID).asString();
  93. } catch (JWTDecodeException e) {
  94. log.error("error:{}", e.getMessage());
  95. return null;
  96. }
  97. }
  98. /**
  99. * @desc : 通过已解密的token获取cpId
  100. * @author : 周兴
  101. * @date : 2023/2/26 16:07
  102. */
  103. public static String getCpId(String token) {
  104. try {
  105. DecodedJWT jwt = JWT.decode(token);
  106. return jwt.getClaim(SHIRO_CP_ID).asString();
  107. } catch (JWTDecodeException e) {
  108. log.error("error:{}", e.getMessage());
  109. return null;
  110. }
  111. }
  112. /**
  113. * @desc : 通过已解密的token获取cpCode
  114. * @author : 周兴
  115. * @date : 2023/2/26 16:07
  116. */
  117. public static String getCpCode(String token) {
  118. try {
  119. DecodedJWT jwt = JWT.decode(token);
  120. return jwt.getClaim(SHIRO_CP_CODE).asString();
  121. } catch (JWTDecodeException e) {
  122. log.error("error:{}", e.getMessage());
  123. return null;
  124. }
  125. }
  126. /**
  127. * @desc : 通过已解密的token获取appCode
  128. * @author : 张潇木
  129. * @date : 2022-7-15 17:34
  130. */
  131. public static String getAppCode(String token) {
  132. try {
  133. DecodedJWT jwt = JWT.decode(token);
  134. return jwt.getClaim(SHIRO_APP_CODE).asString();
  135. } catch (JWTDecodeException e) {
  136. log.error("error:{}", e.getMessage());
  137. return null;
  138. }
  139. }
  140. /**
  141. * @desc : 通过已解密的token获取Lang
  142. * @author : 周兴
  143. * @date : 2022-7-15 17:34
  144. */
  145. public static String getLang(String token) {
  146. try {
  147. DecodedJWT jwt = JWT.decode(token);
  148. return jwt.getClaim(SHIRO_APP_LANG).asString();
  149. } catch (JWTDecodeException e) {
  150. log.error("error:{}", e.getMessage());
  151. return null;
  152. }
  153. }
  154. /**
  155. * @desc : 通过未解密的token获取appCode
  156. * @author : 张潇木
  157. * @date : 2022-7-15 17:34
  158. */
  159. public static String getAppCode(HttpServletRequest httpServletRequest) {
  160. try {
  161. String authorization = httpServletRequest.getHeader("Authorization");
  162. String[] tokens = authorization.split(" ");
  163. String token = AESSecurityUtil.decrypt(ConfigStatic.getAESKey(), tokens[1]);
  164. return JwtUtil.getAppCode(token);
  165. } catch (Exception e) {
  166. log.error("error:{}", e.getMessage());
  167. return null;
  168. }
  169. }
  170. public static String getGrantType(String token) {
  171. try {
  172. DecodedJWT jwt = JWT.decode(token);
  173. return jwt.getClaim(SHIRO_GRANT_TYPE).asString();
  174. } catch (JWTDecodeException e) {
  175. log.error("error:{}", e.getMessage());
  176. return null;
  177. }
  178. }
  179. /**
  180. * 解析token,获取token数据
  181. *
  182. * @param token
  183. * @return
  184. */
  185. public static DecodedJWT getJwtInfo(String token) {
  186. return JWT.decode(token);
  187. }
  188. public static String uuid32() {
  189. return java.util.UUID.randomUUID().toString().replace("-", "");
  190. }
  191. }