springboot-jjwt HS256加解密(PS:驗證就是解密)


最近項目需要用到類似access token進行加解密、驗簽的需求,本人在此做個小筆記記錄一下,以供他人參考。

一共會用到2中加解密,HS256 和 RS256,本文只是對 HS256做個備注,好了直接上代碼,先引入jar包

<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt</artifactId>
    <version>0.9.1</version>
</dependency>

 

核心 Util 類代碼如下:

 

public class JwtUtil { /** * token 過期時間, 單位: 秒. 這個值表示 30 天 */ private static final long TOKEN_EXPIRED_TIME = 30 * 24 * 60 * 60; /** * jwt 加密解密密鑰, 這里請自行賦值,本人暫且使用隨機數16位 */ private static final String JWT_SECRET = "1AsdadSAS123daXX"; public static final String jwtId = "tokenId"; /** * 創建JWT
* @param claims 這個是payLoad的部分內容, jwt格式:jwtHeader.jwtPayLoad.Signature */ public static String createJWT(Map<String, Object> claims, Long time) { SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; //指定簽名的時候使用的簽名算法,也就是header那部分,jjwt已經將這部分內容封裝好了。 Date now = new Date(System.currentTimeMillis()); SecretKey secretKey = generalKey(); long nowMillis = System.currentTimeMillis();//生成JWT的時間 //下面就是在為payload添加各種標准聲明和私有聲明了 JwtBuilder builder = Jwts.builder() //這里其實就是new一個JwtBuilder,設置jwt的body .setClaims(claims) //如果有私有聲明,一定要先設置這個自己創建的私有的聲明,這個是給builder的claim賦值,一旦寫在標准的聲明賦值之后,就是覆蓋了那些標准的聲明的 //.setId(jwtId) //設置jti(JWT ID):是JWT的唯一標識,根據業務需要,這個可以設置為一個不重復的值,主要用來作為一次性token,從而回避重放攻擊。 //.setIssuedAt(now) //iat: jwt的簽發時間
.setHeader("alg", "HS256") //設置header .signWith(signatureAlgorithm, secretKey);//設置簽名使用的簽名算法和簽名使用的秘鑰 if (time >= 0) { long expMillis = nowMillis + time; Date exp = new Date(expMillis); builder.setExpiration(exp); //設置過期時間 } return builder.compact(); } /** * 驗證jwt */ public static Claims verifyJwt(String token) { //簽名秘鑰,和生成的簽名的秘鑰一模一樣 SecretKey key = generalKey(); Claims claims; try { claims = Jwts.parser() //得到DefaultJwtParser .setSigningKey(key) //設置簽名的秘鑰 .parseClaimsJws(token).getBody(); } catch (Exception e) { claims = null; }//設置需要解析的jwt return claims; } /** * 由字符串生成加密key * * @return */ public static SecretKey generalKey() { String stringKey = JWT_SECRET; byte[] encodedKey = Base64.decodeBase64(stringKey); SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "HS256"); return key; } /** * 根據userId和openid生成token */ public static String generateToken() { Map<String, Object> map = new HashMap<>(); map.put("key1", “123ssaa”); map.put("ke2", "123321213"); return createJWT(map, TOKEN_EXPIRED_TIME); } }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM