JWT公私钥解析
⼀⽣成公私钥
public static void main(String[] args) throws NoSuchAlgorithmException {
班级成绩分析
KeyPairGenerator keyPairGenerator = Instance("RSA");
农林致富追忆童年KeyPair keyPair = ateKeyPair();
Key publicKey = Public();
Key privateKey = Private();
System.out.println( (new BASE64Encoder()).Encoded()));
System.out.println( (new BASE64Encoder()).Encoded()));
}
⽣成公私钥备⽤
创意生日蛋糕⼆颁发token项⽬使⽤私钥⽣成
通过BASE64编码转换为私钥
byte[] keyBytes;
keyBytes = Decoder().Bytes());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = Instance("RSA");
atePrivate(keySpec);
通过私钥⽣成数据 claim 为⾃定义数据
public static String ecode(String account, String subject, long expired, Map<String, Object> data, Key privateKey)鼓励鼓励
{
try
{
JSONObject HEADER = JSONObject.parObject("{'type':'JWT','sign':'RS256'}");
Long nowMillis = System.currentTimeMillis();
Date createTime = new Date(nowMillis);
Date expiredTime = new Date(nowMillis + expired);
两代表一委员
JwtBuilder builder = Jwts.builder();
builder.tHeader(HEADER).tId(UUID.randomUUID().toString()).tIssuer("颁发者").tAudience(account).tSubject(subject).tIssuedAt(createTime).tNotBefore(createTime).tExpiration(expiredTime).signWith(SIGNATURE_TYPE, builder.claim("data", data);
pact();
}
吹毛求疵造句
海鲜一锅烩catch (Exception e)
{
e.printStackTrace();
}
return "";
}
三通过公钥获取数据
通过BASE64编码转换为公钥
byte[] keyBytes = Decoder().decode("publicKeyString");
X509EncodedKeySpec keySpec=new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory= Instance("RSA");
通过公钥解析数据
String jwtString = "";
// 使⽤公钥解析JWT内容
Claims claims = dcode(jwtString, atePublic(keySpec));
System.out.Expiration());
System.out.println(new Date());
System.out.Expiration());
System.out.("data"));
System.out.println();
解析token decode
public Claims decode(String jwtToken,PublicKey publicKey){
return (Claims) Jwts.parr().tSigningKey(publicKey).par(jwtToken).getBody(); }