簡單的JWT示例


相關POM引用

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

 

相關代碼

package com.dhh;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**@author denghanghui
 * @Date 2020 03 06
 * Json Web Token  簡單示例
 */
public class TWJTemple {

    public static void main(String[] args) {
        SignatureAlgorithm signatureAlgorithm = getSignatureAlgorithm();
        SecretKey key = getKey();

        //設置啟動時間和過期時間
        long nowMillis = System.currentTimeMillis();
        long expMillis = nowMillis + 300000;
        Date now = new Date(nowMillis);
        Date expDate = new Date(expMillis);

        //JWT中存的信息
        Map map=new HashMap();
        map.put("username","yunnengkeji");
        map.put("password","123456789");

        JwtBuilder builder = Jwts.builder();
        builder.setId("1").setSubject("jwt").setIssuer("denghanghui").setIssuedAt(now).signWith(signatureAlgorithm,key).setExpiration(expDate).addClaims(map);

        //獲取koken
        String token= builder.compact();
        Claims claims=null;
        //解析token內的容器
        try {
            claims =  parseJWT(token);
            Object username= claims.get("username");
            Object password= claims.get("password");
            System.out.println("*****************************************************************");
            System.out.println(username);
            System.out.println(password);
            System.out.println("*****************************************************************");
        }catch (Exception e){
            System.out.println("解析異常");
        }
    }

    /**
     * 解析JWT字符串獲取Claims容器
     * @param jwt JWT字符串
     * @return Claims容器
     * @throws Exception 解析異常
     */
    public static Claims parseJWT(String jwt) throws Exception {
        SecretKey secretKey = getParseKey();
        return Jwts.parser()
                .setSigningKey(secretKey)
                .parseClaimsJws(jwt)
                .getBody();
    }

    /**
     * 獲取解析用密鑰
     * @return 解析用密鑰對象
     */
    public static SecretKey getParseKey(){
        byte[] encodedKey = Base64.decodeBase64("denghanghui");
        return new SecretKeySpec(encodedKey, 0, encodedKey.length, "");
    }

    /**
     * 獲取加密用密鑰
     * @return 加密用密鑰對象
     */
    public static SecretKey getKey(){
        byte[] encodedKey = Base64.decodeBase64("denghanghui");
        return new SecretKeySpec(encodedKey, 0, encodedKey.length, "");
    }


    /**
     * 設置加密簽名算法
     * @return 設置加密簽名算法
     */
    public static SignatureAlgorithm getSignatureAlgorithm(){
        return SignatureAlgorithm.HS256;
    }
}

 


免責聲明!

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



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