| import io.jsonwebtoken.*; |
| |
| import javax.crypto.SecretKey; |
| import javax.crypto.spec.SecretKeySpec; |
| import java.util.Base64; |
| import java.util.Date; |
| import java.util.UUID; |
| |
| public class JWTHelper { |
| |
| public static final Long JWT_TTL = 60 * 60 * 1000L; |
| |
| public static final String JWT_KEY = "liusangbaoyo"; |
| |
| public static String getUUID() { |
| return UUID.randomUUID().toString().replaceAll("-", ""); |
| } |
| |
| |
| * 生成 JWT |
| * |
| * @param subject token 中要存放的数据 (JSON 格式) |
| * @return |
| */ |
| public static String createJwt(String subject) { |
| return getJwtBuilder(subject, null, getUUID(), null, null).compact(); |
| } |
| |
| public static String createJwt(Long id, String name) { |
| return getJwtBuilder(null, null, getUUID(), id, name).compact(); |
| } |
| |
| public static String createJwt(String subject, Long ttlMillis) { |
| |
| return getJwtBuilder(subject, ttlMillis, getUUID(), null, null).compact(); |
| } |
| |
| private static JwtBuilder getJwtBuilder(String subject, Long ttlMillis, String uuid, Long id, String name) { |
| SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; |
| SecretKey secretKey = generalKey(); |
| long nowMillis = System.currentTimeMillis(); |
| Date now = new Date(nowMillis); |
| if (ttlMillis == null) { |
| ttlMillis = JWTHelper.JWT_TTL; |
| } |
| long expMills = nowMillis + ttlMillis; |
| Date expDate = new Date(expMills); |
| return Jwts.builder() |
| .setId(uuid) |
| .setSubject(subject) |
| .claim("userId", id) |
| .claim("username", name) |
| .setIssuer("dkx") |
| .setIssuedAt(now) |
| .signWith(signatureAlgorithm, secretKey) |
| .setExpiration(expDate); |
| } |
| |
| public static String createJwt(String id, String subject, Long ttlMillis) { |
| return getJwtBuilder(subject, ttlMillis, id, null, null).compact(); |
| } |
| |
| private static SecretKey generalKey() { |
| byte[] encodedKey = Base64.getDecoder().decode(JWTHelper.JWT_KEY); |
| return new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES"); |
| } |
| |
| public static Claims parseJwt(String jwt) { |
| Claims claims; |
| SecretKey secretKey = generalKey(); |
| try { |
| return Jwts.parser() |
| .setSigningKey(secretKey) |
| .parseClaimsJws(jwt) |
| .getBody(); |
| } catch (ExpiredJwtException e) { |
| claims = e.getClaims(); |
| } |
| |
| return claims; |
| } |
| } |