TokenUtils.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.zhentao.utils;
  2. import com.zhentao.common.exception.RRException;
  3. import io.jsonwebtoken.Claims;
  4. import io.jsonwebtoken.JwtBuilder;
  5. import io.jsonwebtoken.Jwts;
  6. import io.jsonwebtoken.SignatureAlgorithm;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import javax.xml.bind.DatatypeConverter;
  9. import java.security.Key;
  10. import java.util.Date;
  11. /**
  12. * 生成Token工具类
  13. */
  14. public class TokenUtils {
  15. /**
  16. * 签名秘钥
  17. */
  18. public static final String SECRET = "cjyfutu1688";
  19. /**
  20. * 生成token
  21. * @param id 一般传入userName
  22. * @return
  23. */
  24. public static String createJwtToken(String id){
  25. String issuer = "www.futureading.com";
  26. String subject = "65532781@qq.com";
  27. long ttlMillis = System.currentTimeMillis();
  28. return createJwtToken(id, issuer, subject, ttlMillis);
  29. }
  30. /**
  31. * 生成Token
  32. *
  33. * @param id
  34. * 编号
  35. * @param issuer
  36. * 该JWT的签发者,是否使用是可选的
  37. * @param subject
  38. * 该JWT所面向的用户,是否使用是可选的;
  39. * @param ttlMillis
  40. * 签发时间
  41. * @return token String
  42. */
  43. public static String createJwtToken(String id, String issuer, String subject, long ttlMillis) {
  44. // 签名算法 ,将对token进行签名
  45. SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
  46. // 生成签发时间
  47. long nowMillis = System.currentTimeMillis();
  48. Date now = new Date(nowMillis);
  49. // 通过秘钥签名JWT
  50. byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(SECRET);
  51. Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
  52. // Let's set the JWT Claims
  53. JwtBuilder builder = Jwts.builder().setId(id)
  54. .setIssuedAt(now)
  55. .setSubject(subject)
  56. .setIssuer(issuer)
  57. .signWith(signatureAlgorithm, signingKey);
  58. // if it has been specified, let's add the expiration
  59. if (ttlMillis >= 0) {
  60. long expMillis = nowMillis + ttlMillis;
  61. Date exp = new Date(expMillis);
  62. builder.setExpiration(exp);
  63. }
  64. // Builds the JWT and serializes it to a compact, URL-safe string
  65. return builder.compact();
  66. }
  67. // Sample method to validate and read the JWT
  68. public static Claims parseJWT(String jwt) {
  69. // This line will throw an exception if it is not a signed JWS (as expected)
  70. Claims claims = Jwts.parser()
  71. .setSigningKey(DatatypeConverter.parseBase64Binary(SECRET))
  72. .parseClaimsJws(jwt).getBody();
  73. return claims;
  74. }
  75. public static Long getUserId(String token){
  76. Claims claims = null;
  77. try {
  78. claims = TokenUtils.parseJWT(token);
  79. } catch (Exception e) {
  80. throw new RRException("token错误,请重新登录",101);
  81. }
  82. if(null==claims) {
  83. throw new RRException("token错误,请重新登录",101);
  84. }
  85. String id = claims.getId();
  86. Long userId=Long.valueOf(id);
  87. return userId;
  88. }
  89. public static void main(String[] args) {
  90. System.out.println(TokenUtils.createJwtToken("admin"));
  91. }
  92. }