JwtUtils.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.yaozhitech.spring5.utils;
  2. import java.io.UnsupportedEncodingException;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.Map;
  6. import com.auth0.jwt.JWT;
  7. import com.auth0.jwt.JWTVerifier;
  8. import com.auth0.jwt.algorithms.Algorithm;
  9. import com.auth0.jwt.exceptions.JWTDecodeException;
  10. import com.auth0.jwt.interfaces.Claim;
  11. import com.auth0.jwt.interfaces.DecodedJWT;
  12. import com.yaozhitech.spring5.common.util.UUIDUtils;
  13. import lombok.extern.slf4j.Slf4j;
  14. @Slf4j
  15. public class JwtUtils {
  16. /**
  17. * 获得token中的信息无需secret解密也能获得
  18. * @return token中包含的签发时间
  19. */
  20. public static Date getIssuedAt(String token) {
  21. try {
  22. DecodedJWT jwt = JWT.decode(token);
  23. return jwt.getIssuedAt();
  24. } catch (JWTDecodeException e) {
  25. log.error(e.getMessage(), e);
  26. return null;
  27. }
  28. }
  29. /**
  30. * 获得token中的信息无需secret解密也能获得
  31. * @return token中包含的用户名
  32. */
  33. public static String getUsername(String token) {
  34. try {
  35. DecodedJWT jwt = JWT.decode(token);
  36. return jwt.getClaim("username").asString();
  37. } catch (JWTDecodeException e) {
  38. log.error(e.getMessage(), e);
  39. return null;
  40. }
  41. }
  42. /**
  43. * 需要密钥才能获得信息
  44. */
  45. public static Map<String, Claim> verifyToken(String token, String secret) {
  46. DecodedJWT jwt = null;
  47. try {
  48. JWTVerifier verifier = JWT.require(Algorithm.HMAC256(secret)).build();
  49. jwt = verifier.verify(token);
  50. } catch (Exception e) {
  51. log.error(e.getMessage(), e);
  52. return null;
  53. }
  54. return jwt.getClaims();
  55. }
  56. public static String verifyTokenAndGet(String token, String secret) {
  57. DecodedJWT jwt = null;
  58. try {
  59. JWTVerifier verifier = JWT.require(Algorithm.HMAC256(secret)).build();
  60. jwt = verifier.verify(token);
  61. } catch (Exception e) {
  62. log.error(e.getMessage(), e);
  63. return null;
  64. }
  65. return jwt.getClaim("username").asString();
  66. }
  67. /**
  68. * 生成签名,expireTime后过期
  69. * @param username 用户名
  70. * @param time 过期时间s
  71. * @return 加密的token
  72. */
  73. public static String sign(String username, String salt, long time) {
  74. try {
  75. Date date = new Date(System.currentTimeMillis()+time*1000);
  76. Algorithm algorithm = Algorithm.HMAC256(salt);
  77. // 附带username信息
  78. return JWT.create()
  79. .withClaim("username", username)
  80. .withExpiresAt(date)
  81. .withIssuedAt(new Date())
  82. .sign(algorithm);
  83. } catch (UnsupportedEncodingException e) {
  84. return null;
  85. }
  86. }
  87. /**
  88. * token是否过期
  89. * @return true:过期
  90. */
  91. public static boolean isTokenExpired(String token) {
  92. Date now = Calendar.getInstance().getTime();
  93. DecodedJWT jwt = JWT.decode(token);
  94. return jwt.getExpiresAt().before(now);
  95. }
  96. /**
  97. * 生成随机盐,长度32位
  98. * @return
  99. */
  100. public static String generateSalt(){
  101. return UUIDUtils.generateShortUuid();
  102. }
  103. }