jwt 工具類


public class TokenUtils {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 簽名秘鑰
     */
    public static final String SECRET = "token";

    /**
     * 生成token
     * @param id 一般傳入userName
     * @return
     */
    public static String createJwtToken(String id){
        String issuer = "";
        String subject = "";
        long ttlMillis = 30*60*1000; 
        return createJwtToken(id, issuer, subject, ttlMillis);
    }

    /**
     * 生成Token
     * 
     * @param id
     *            編號
     * @param issuer
     *            該JWT的簽發者,是否使用是可選的
     * @param subject
     *            該JWT所面向的用戶,是否使用是可選的;
     * @param ttlMillis
     *            簽發時間
     * @return token String
     */
    public static String createJwtToken(String id, String issuer, String subject, long ttlMillis) {

        // 簽名算法 ,將對token進行簽名
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;

        // 生成簽發時間
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);

        // 通過秘鑰簽名JWT
        byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(SECRET);
        Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());

        // Let's set the JWT Claims
        JwtBuilder builder = Jwts.builder().setId(id)
            .setIssuedAt(now)
            .setSubject(subject)
            .setIssuer(issuer)
            .signWith(signatureAlgorithm, signingKey);
        
        // if it has been specified, let's add the expiration
        if (ttlMillis >= 0) {
            long expMillis = nowMillis + ttlMillis;
            Date exp = new Date(expMillis);
            builder.setExpiration(exp);
        }

        // Builds the JWT and serializes it to a compact, URL-safe string
        return builder.compact();

    }

    // Sample method to validate and read the JWT
    public static Claims parseJWT(String jwt) {
        // This line will throw an exception if it is not a signed JWS (as expected)
        Claims claims = Jwts.parser()
            .setSigningKey(DatatypeConverter.parseBase64Binary(SECRET))
            .parseClaimsJws(jwt).getBody();
        return claims;
    }

    public static void main(String[] args) {
        System.out.println(TokenUtils.createJwtToken("admin"));
        System.out.println(TokenUtils.parseJWT("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJhZG1pbiIsImlhdCI6MTUxNzg4NDU0OCwic3ViIjoiIiwiaXNzIjoiIiwiZXhwIjoxNTE3ODg2MzQ4fQ.AxGGAOiwOmnp5oKaSdlmny-zgZigsqgZY8fG_UxVLKs"));
    }
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數湊字數


免責聲明!

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



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