Java筆記-使用jjwt生成jwt

jwt的全稱爲: JSON WEB TOKEN

程序運行截圖如下:

maven添加:

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

代碼如下:

package cn.it1995.qqServer.util;

import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.tomcat.util.codec.binary.Base64;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
public class JwtUtil {

    //加密 解密時的密鑰 用來生成key
    public static final String JWT_KEY = "IT1995";

    /**
     * 由字符串生成加密的key
     * @return
     */
    public static SecretKeySpec generalKey(){

        String stringKey = JWT_KEY;
        byte[] bytes = Base64.decodeBase64(stringKey);
        SecretKeySpec key = new SecretKeySpec(bytes, 0, bytes.length, "AES");
        return key;
    }

    public static String createJWT(String id, String subject, long ttlMillis){

        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; //指定簽名的時候使用的簽名算法,也就是header那部分,jjwt已經將這部分內容封裝好了。
        long nowMillis = System.currentTimeMillis();//生成JWT的時間
        Date now = new Date(nowMillis);
        SecretKey key = generalKey();//生成簽名的時候使用的祕鑰secret,這個方法本地封裝了的,一般可以從本地配置文件中讀取,切記這個祕鑰不能外露哦。它就是你服務端的私鑰,在任何場景都不應該流露出去。一旦客戶端得知這個secret, 那就意味着客戶端是可以自我簽發jwt了。
        JwtBuilder builder = Jwts.builder() //這裏其實就是new一個JwtBuilder,設置jwt的body
//                .setClaims(claims)            //如果有私有聲明,一定要先設置這個自己創建的私有的聲明,這個是給builder的claim賦值,一旦寫在標準的聲明賦值之後,就是覆蓋了那些標準的聲明的
                .setId(id)                    //設置jti(JWT ID):是JWT的唯一標識,根據業務需要,這個可以設置爲一個不重複的值,主要用來作爲一次性token,從而回避重放攻擊。
                .setIssuedAt(now)            //iat: jwt的簽發時間
                .setSubject(subject)        //sub(Subject):代表這個JWT的主體,即它的所有人,這個是一個json格式的字符串,可以存放什麼userid,roldid之類的,作爲什麼用戶的唯一標誌。
                .signWith(signatureAlgorithm, key);//設置簽名使用的簽名算法和簽名使用的祕鑰
        if (ttlMillis >= 0) {
            long expMillis = nowMillis + ttlMillis;
            Date exp = new Date(expMillis);
            builder.setExpiration(exp);        //設置過期時間
        }
        return builder.compact();            //就開始壓縮爲xxxxxxxxxxxxxx.xxxxxxxxxxxxxxx.xxxxxxxxxxxxx這樣的jwt
    }

    public static Claims parseJWT(String jwt){

        SecretKey key = generalKey();  //簽名祕鑰,和生成的簽名的祕鑰一模一樣
        Claims claims = Jwts.parser()  //得到DefaultJwtParser
                .setSigningKey(key)         //設置簽名的祕鑰
                .parseClaimsJws(jwt).getBody();//設置需要解析的jwt
        return claims;
    }

    public static void main(String[] args){

        Map<String, Object> user = new HashMap<>();
        user.put("username", "it1995");
        user.put("password", "123456");
        String jwt = createJWT(UUID.randomUUID().toString(), JSON.toJSONString(user), 3600 * 24);

        System.out.println("加密後:" + jwt);

        //解密
        Claims claims = parseJWT(jwt);
        System.out.println("解密後:" + claims.getSubject());
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章