小编Kis*_*tty的帖子

如何使用 RSA512 算法创建签名的 JWT

我试图创建 JWT(“JOT”)令牌以使我的 api 调用真实。当我尝试使用 RSA512 签名创建令牌时,我收到一条错误消息

java.lang.IllegalArgumentException:必须使用 RSA PrivateKey 计算 RSA 签名。指定的 javax.crypto.spec.SecretKeySpec 类型的密钥不是 RSA PrivateKey。

我正在使用以下代码:

 SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.RS512;

 long nowMillis = System.currentTimeMillis();
  Date now = new Date(nowMillis);
 byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(SECRET_KEY);
 Key signingKey = new SecretKeySpec(apiKeySecretBytes, 
                signatureAlgorithm.getJcaName());

   JwtBuilder builder = Jwts.builder().claim("uuid", 
    id).setIssuedAt(now).setExpiration(new Date(600000))
    .signWith(signatureAlgorithm, signingKey);
Run Code Online (Sandbox Code Playgroud)

注意:我的“SECRET_KEY”是一个字符串,它是一个在线随机生成的私钥。我的问题是如何从使用 RSA 密钥大小编码为 4096. 4096 的字符串中获取 Key 对象,因为我使用的是 RSA512 加密,建议对 RSA512 使用 4096 密钥

java jwt spring-boot jwt-auth

5
推荐指数
1
解决办法
3528
查看次数

标签 统计

java ×1

jwt ×1

jwt-auth ×1

spring-boot ×1