You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package cc.aspark.utils ;
import io.jsonwebtoken.Claims ;
import io.jsonwebtoken.JwtBuilder ;
import io.jsonwebtoken.Jwts ;
import io.jsonwebtoken.SignatureAlgorithm ;
import java.nio.charset.StandardCharsets ;
import java.util.Date ;
import java.util.Map ;
public class JwtUtil {
/**
* 生成jwt
* 使用Hs256算法, 私匙使用固定秘钥
*
* @param secretKey jwt秘钥
* @param ttlMillis jwt过期时间(毫秒)
* @param claims 设置的信息
* @return
*/
public static String createJWT ( String secretKey , long ttlMillis , Map < String , Object > claims ) {
// 指定签名的时候使用的签名算法, 也就是header那部分
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm . HS256 ;
// 生成JWT的时间
long expMillis = System . currentTimeMillis ( ) + ttlMillis ;
Date exp = new Date ( expMillis ) ;
// 设置jwt的body
JwtBuilder builder = Jwts . builder ( )
. setClaims ( claims )
. signWith ( signatureAlgorithm , secretKey . getBytes ( StandardCharsets . UTF_8 ) )
. setExpiration ( exp ) ;
return builder . compact ( ) ;
}
/**
* Token解密
*
* @param secretKey jwt秘钥 此秘钥一定要保留好在服务端, 不能暴露出去, 否则sign就可以被伪造, 如果对接多个客户端建议改造成多个
* @param token 加密后的token
* @return
*/
public static Claims parseJWT ( String secretKey , String token ) {
// 得到DefaultJwtParser
return Jwts . parser ( )
. setSigningKey ( secretKey . getBytes ( StandardCharsets . UTF_8 ) )
. parseClaimsJws ( token ) . getBody ( ) ;
}
}