All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eu.vooo.commons.security.util.JJWTUtils Maven / Gradle / Ivy

The newest version!
package org.eu.vooo.commons.security.util;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.eu.vooo.commons.security.JJWTParseException;

import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @author [email protected]
 */
public class JJWTUtils {
    public static String generate(String subject, String privateKeyStr) {
        PrivateKey privateKey = RSAUtils.privateKey(privateKeyStr);
        return Jwts.builder()
                .setSubject(subject)
                .signWith(privateKey, SignatureAlgorithm.RS256)
                .compact();
    }

    public static String parse(String token, String publicKeyStr) throws JJWTParseException {
        PublicKey publicKey = RSAUtils.publicKey(publicKeyStr);
        String subject;
        try {
            subject = Jwts.parserBuilder().setSigningKey(publicKey).build().parseClaimsJws(token).getBody().getSubject();
        } catch (Exception e) {
            throw new JJWTParseException("token解析异常");
        }
        return subject;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy