用于JJWT令牌生成的私钥

时间:2020-05-08 00:35:45

标签: java jwt

我正在尝试生成一个JWT,它引用了https://stormpath.com/blog/jwt-java-create-verify

之类的文档

但是,我得到一个错误

java.lang.IllegalArgumentException: RSA signatures be computed using a PrivateKey.Object of class [javax.crypto.spec.SecretKeySpec] must be an instance of interface java.security.PrivateKey

此错误发生在builder.compact()。

我想知道这是否是因为signingKey是Typed Key而不是PrivateKey,但是键入或强制转换会引发其他错误。

下面是我的代码。

public static String createJWT() {

    //The JWT signature algorithm we will be using to sign the token
    SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.RS256;



    //We will sign our JWT with our ApiKey secret
    byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(SECRET_KEY);
    Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());

    //Let's set the JWT Claims

    JwtBuilder builder = Jwts.builder()
        .setNotBefore(new Date())
        .setIssuedAt(new Date())
        .setExpiration(new Date())
        .signWith(signatureAlgorithm, signingKey);


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

0 个答案:

没有答案