JwtUtil.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package com.dk.oauth.util;
  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.constant.OauthConstants;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest;
  10. import javax.servlet.http.HttpServletRequest;
  11. import java.util.Date;
  12. @Slf4j
  13. public class JwtUtil {
  14. public static final String SHIRO_USER_NAME = "username";
  15. public static final String SHIRO_USER_ID = "userId";
  16. public static final String SHIRO_USER_WX_ID = "userWxId";
  17. public static final String SHIRO_CP_ID = "cpId";
  18. public static final String SHIRO_CP_CODE = "cpCode";
  19. public static final String SHIRO_CLIENT_ID = "clientId";
  20. public static final String SHIRO_APP_CODE = "appCode";
  21. public static final String SHIRO_USER_SALT = "salt";
  22. public static final String SHIRO_ISSUER = "Issuer";
  23. public static final String SHIRO_SUBJECT = "long_token";
  24. public static final String SHIRO_GRANT_TYPE = "grantType";
  25. public static final String SHIRO_APP_LANG = "lang";
  26. /**
  27. * 校验token是否正确
  28. *
  29. * @param token 密钥
  30. * @param salt 盐值
  31. * @return 是否正确
  32. */
  33. public static boolean verify(String token, String salt) {
  34. try {
  35. Algorithm algorithm = Algorithm.HMAC256(salt);
  36. JWTVerifier verifier = JWT.require(algorithm)
  37. // 签发人
  38. .withIssuer(SHIRO_ISSUER)
  39. // 主题
  40. .withSubject(SHIRO_SUBJECT)
  41. // 签发的目标
  42. //.withAudience(jwtProperties.getAudience())
  43. .build();
  44. DecodedJWT jwt = verifier.verify(token);
  45. if (jwt != null) {
  46. return true;
  47. }
  48. } catch (Exception e) {
  49. log.error("The token is invalid{}", e.getMessage());
  50. }
  51. return false;
  52. }
  53. /**
  54. * 获取AES解密token
  55. *
  56. * @param request
  57. * @return
  58. */
  59. public static String getDecryptToken(HttpServletRequest request, String AESKey) {
  60. String token = "";
  61. try {
  62. // 构建 OAuth2 资源请求
  63. OAuthAccessResourceRequest oauthRequest = new OAuthAccessResourceRequest(request);
  64. // 获取Access Token
  65. String accessToken = oauthRequest.getAccessToken();
  66. token = AESSecurityUtil.decrypt(AESKey, accessToken);
  67. return token;
  68. } catch (Exception e) {
  69. throw new RuntimeException(e);
  70. }
  71. }
  72. /**
  73. * @desc : 获取token中UserName
  74. * @author : 周兴
  75. * @date : 2023/2/26 16:32
  76. */
  77. public static String getUserName(String token) {
  78. try {
  79. DecodedJWT jwt = JWT.decode(token);
  80. return jwt.getClaim(SHIRO_USER_NAME).asString();
  81. } catch (JWTDecodeException e) {
  82. log.error("error:{}", e.getMessage());
  83. return null;
  84. }
  85. }
  86. /**
  87. * @desc : 获取token中UserId
  88. * @author : 周兴
  89. * @date : 2023/2/26 16:32
  90. */
  91. public static String getUserId(String token) {
  92. try {
  93. DecodedJWT jwt = JWT.decode(token);
  94. return jwt.getClaim(SHIRO_USER_ID).asString();
  95. } catch (JWTDecodeException e) {
  96. log.error("error:{}", e.getMessage());
  97. return null;
  98. }
  99. }
  100. /**
  101. * @desc : 获取token中UserWxId
  102. * @author : 周兴
  103. * @date : 2023/2/26 16:32
  104. */
  105. public static String getUserWxId(String token) {
  106. try {
  107. DecodedJWT jwt = JWT.decode(token);
  108. return jwt.getClaim(SHIRO_USER_WX_ID).asString();
  109. } catch (JWTDecodeException e) {
  110. log.error("error:{}", e.getMessage());
  111. return null;
  112. }
  113. }
  114. /**
  115. * @desc : 获取token中CpId
  116. * @author : 周兴
  117. * @date : 2023/2/26 16:32
  118. */
  119. public static String getCPId(String token) {
  120. try {
  121. DecodedJWT jwt = JWT.decode(token);
  122. return jwt.getClaim(SHIRO_CP_ID).asString();
  123. } catch (JWTDecodeException e) {
  124. log.error("error:{}", e.getMessage());
  125. return null;
  126. }
  127. }
  128. /**
  129. * @desc : 获取token中CpCode
  130. * @author : 周兴
  131. * @date : 2023/2/26 16:32
  132. */
  133. public static String getCpCode(String token) {
  134. try {
  135. DecodedJWT jwt = JWT.decode(token);
  136. return jwt.getClaim(SHIRO_CP_CODE).asString();
  137. } catch (JWTDecodeException e) {
  138. log.error("error:{}", e.getMessage());
  139. return null;
  140. }
  141. }
  142. /**
  143. * @desc : 获取token中Lang
  144. * @author : 周兴
  145. * @date : 2023/2/26 16:32
  146. */
  147. public static String getLang(String token) {
  148. try {
  149. DecodedJWT jwt = JWT.decode(token);
  150. return jwt.getClaim(SHIRO_APP_LANG).asString();
  151. } catch (JWTDecodeException e) {
  152. log.error("error:{}", e.getMessage());
  153. return null;
  154. }
  155. }
  156. public static String getGrantType(String token) {
  157. try {
  158. DecodedJWT jwt = JWT.decode(token);
  159. return jwt.getClaim(SHIRO_GRANT_TYPE).asString();
  160. } catch (JWTDecodeException e) {
  161. log.error("error:{}", e.getMessage());
  162. return null;
  163. }
  164. }
  165. /**
  166. * 解析token,获取token数据
  167. *
  168. * @param token
  169. * @return
  170. */
  171. public static DecodedJWT getJwtInfo(String token) {
  172. return JWT.decode(token);
  173. }
  174. /**
  175. * 生成TOKEN,24小时后过期
  176. *
  177. * @param username 用户名
  178. * @param salt 盐值
  179. * @return 加密的token
  180. */
  181. public static String sign(String username, String userId, String userWxId, String appCode, String clientId, String salt, String cpId, String cpCode, String lang) {
  182. Date expireDate = new Date(System.currentTimeMillis() + OauthConstants.EXPIRES_IN);
  183. //加盐值
  184. Algorithm algorithm = Algorithm.HMAC256(salt);
  185. // 附带username信息
  186. return JWT.create()
  187. .withClaim(SHIRO_USER_NAME, username)
  188. .withClaim(SHIRO_USER_ID, userId)
  189. .withClaim(SHIRO_USER_WX_ID, userWxId)
  190. .withClaim(SHIRO_CP_ID, cpId)
  191. .withClaim(SHIRO_CP_CODE, cpCode)
  192. .withClaim(SHIRO_APP_CODE, appCode)
  193. .withClaim(SHIRO_CLIENT_ID, clientId)
  194. .withClaim(SHIRO_USER_SALT, salt)
  195. .withClaim(SHIRO_APP_LANG, lang)
  196. // jwt唯一id
  197. .withJWTId(uuid32())
  198. // 签发人
  199. .withIssuer(SHIRO_ISSUER)
  200. // 主题
  201. .withSubject(SHIRO_SUBJECT)
  202. // 签发的目标
  203. //.withAudience(jwtProperties.getAudience())
  204. // 签名时间
  205. .withIssuedAt(new Date())
  206. // token过期时间
  207. .withExpiresAt(expireDate)
  208. // 签名
  209. .sign(algorithm);
  210. }
  211. public static String sign(String username, String userId, String userWxId, String appCode, String clientId, String salt, String grantType, String cpId, String cpCode, String lang) {
  212. Date expireDate = new Date(System.currentTimeMillis() + OauthConstants.EXPIRES_IN);
  213. //加盐值
  214. Algorithm algorithm = Algorithm.HMAC256(salt);
  215. // 附带username信息
  216. return JWT.create()
  217. .withClaim(SHIRO_USER_NAME, username)
  218. .withClaim(SHIRO_USER_ID, userId)
  219. .withClaim(SHIRO_USER_WX_ID, userWxId)
  220. .withClaim(SHIRO_CP_ID, cpId)
  221. .withClaim(SHIRO_CP_CODE, cpCode)
  222. .withClaim(SHIRO_APP_CODE, appCode)
  223. .withClaim(SHIRO_CLIENT_ID, clientId)
  224. .withClaim(SHIRO_USER_SALT, salt)
  225. .withClaim(SHIRO_GRANT_TYPE, grantType)
  226. .withClaim(SHIRO_APP_LANG, lang)
  227. // jwt唯一id
  228. .withJWTId(uuid32())
  229. // 签发人
  230. .withIssuer(SHIRO_ISSUER)
  231. // 主题
  232. .withSubject(SHIRO_SUBJECT)
  233. // 签发的目标
  234. //.withAudience(jwtProperties.getAudience())
  235. // 签名时间
  236. .withIssuedAt(new Date())
  237. // token过期时间
  238. .withExpiresAt(expireDate)
  239. // 签名
  240. .sign(algorithm);
  241. }
  242. public static String sign(String username, String clientId, String salt, String cpId, String cpCode, String lang) {
  243. Date expireDate = new Date(System.currentTimeMillis() + OauthConstants.EXPIRES_IN);
  244. //加盐值
  245. Algorithm algorithm = Algorithm.HMAC256(salt);
  246. // 附带username信息
  247. return JWT.create()
  248. .withClaim(SHIRO_USER_NAME, username)
  249. .withClaim(SHIRO_CP_ID, cpId)
  250. .withClaim(SHIRO_CP_CODE, cpCode)
  251. .withClaim(SHIRO_CLIENT_ID, clientId)
  252. .withClaim(SHIRO_USER_SALT, salt)
  253. .withClaim(SHIRO_APP_LANG, lang)
  254. // jwt唯一id
  255. .withJWTId(uuid32())
  256. // 签发人
  257. .withIssuer(SHIRO_ISSUER)
  258. // 主题
  259. .withSubject(SHIRO_SUBJECT)
  260. // 签发的目标
  261. //.withAudience(jwtProperties.getAudience())
  262. // 签名时间
  263. .withIssuedAt(new Date())
  264. // token过期时间
  265. .withExpiresAt(expireDate)
  266. // 签名
  267. .sign(algorithm);
  268. }
  269. public static String uuid32() {
  270. return java.util.UUID.randomUUID().toString().replace("-", "");
  271. }
  272. }