【问题标题】:Base64-encoded key bytes may only be specified for HMAC signatures只能为 HMAC 签名指定 Base64 编码的密钥字节
【发布时间】:2018-10-16 11:51:57
【问题描述】:

您好,我正在使用 Spring Security 在 Spring Boot 中编写 JWT。当我在正文部分使用以下详细信息向邮递员请求 POST 时

{
"userName": "RAM",
"id":123,
"role": "admin"
}

然后我得到以下错误

{
    "timestamp": "2018-05-06T14:57:12.048+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Base64-encoded key bytes may only be specified for HMAC signatures.  If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.",
    "path": "/token"
}

我在下面的代码中使用了生成器的 jwt builder

   @Component
public class JwtGenerator {

    public String generate(JwtUser jwtUser) {
        // TODO Auto-generated method stub
        Claims claim= Jwts.claims() 
                .setSubject(jwtUser.getUserName());
            claim.put("userId", String.valueOf(jwtUser.getId()));
            claim.put("role", jwtUser.getRole());

            String secret = "YouTube";

            byte[] bytesEncoded = Base64.getEncoder().encode(secret.getBytes());

        return  Jwts.builder().setClaims(claim).signWith(SignatureAlgorithm.ES512, secret).compact();
                //With(SignatureAlgorithm.ES512, bytesEncoded).compact();
                //signWith(SignatureAlgorithm.ES512,"YouTube").compact();

    }

}

我使用直接字符串值作为密钥和其他两种可能的组合,但无法找出问题所在。我还从下面的代码中提供了 JwtBuilder 中 DefaultJwtBuilder 所期望的编码字符串,仍然没有命中。

 @Override
    public JwtBuilder signWith(SignatureAlgorithm alg, String base64EncodedSecretKey) {
        Assert.hasText(base64EncodedSecretKey, "base64-encoded secret key cannot be null or empty.");
        Assert.isTrue(alg.isHmac(), "Base64-encoded key bytes may only be specified for HMAC signatures.  If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.");
        byte[] bytes = TextCodec.BASE64.decode(base64EncodedSecretKey);
        return signWith(alg, bytes);
    }

任何帮助将不胜感激。

【问题讨论】:

    标签: spring-boot spring-security base64 jwt


    【解决方案1】:

    你从“SignatureAlgorithm.ES512, secret”切换到“SignatureAlgorithm.HS512, secret”,只需要用户名&&密码

    【讨论】:

      【解决方案2】:

      您代码中的签名算法是使用椭圆曲线算法的 ES512。由于您使用的是密钥,因此您希望使用前缀为“HS”的 HMAC 算法。所以 HS256、HS384 或 HS512。

      更改
      Jwts.builder().setClaims(claim).signWith(SignatureAlgorithm.ES512, secret).compact();


      Jwts.builder().setClaims(claim).signWith(SignatureAlgorithm.HS512, secret).compact();

      【讨论】:

      • 为我工作!这应该被标记为正确答案!
      • 很高兴记住使用密钥真的很长;)
      猜你喜欢
      • 2020-05-19
      • 1970-01-01
      • 2022-10-18
      • 2011-08-06
      • 2013-07-11
      • 1970-01-01
      • 2019-07-19
      • 2017-01-30
      • 2022-09-25
      相关资源
      最近更新 更多