1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package com.zfxt.sjv.util;
import io.jsonwebtoken.*;
import java.util.Date;
public class JwtUtil { private static long time = 1000 * 60 * 24; private static String signature = "zfx-t";
public static String encrypt(){ JwtBuilder jwtBuilder = Jwts.builder(); String token = jwtBuilder .setHeaderParam("typ","JWT") .setHeaderParam("alg","HS256") .claim("username","admin") .claim("password","admin") .setExpiration(new Date(System.currentTimeMillis()+time)) .signWith(SignatureAlgorithm.HS256,signature) .compact(); return token; } public void decrypt(){ String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwicm9sZSI6ImFkbWluIiwic3ViIjoiSldULXRlc3QiLCJleHAiOjE2ODE4NzU3ODYsImp0aSI6IjkyNDY3N2EwLTBmNDEtNGEwMi04NWY1LTVmMGMxYTY5Nzg2MyJ9.BsHrHcWcHCoWtJVdAuybeAEG1FSUpx1aUDApnEQfAkA"; JwtParser jwtParser = Jwts.parser(); Jws<Claims> claimsJws = jwtParser.setSigningKey(signature).parseClaimsJws(token); Claims claims = claimsJws.getBody(); System.out.println(claims.get("username")); System.out.println(claims.getId()); System.out.println(claims.getSubject()); System.out.println(claims.getExpiration());
} }
|