【问题标题】:Can't make ES256 JWT in java无法在 Java 中制作 ES256 JWT
【发布时间】:2020-04-20 18:29:13
【问题描述】:

我无法使用 ES256 算法制作 JWT,我不知道为什么。 这是我的代码:

Map<String, Object> headerMap = new HashMap<String, Object>();

headerMap.put("kid", keyId);
headerMap.put("alg", "ES256");
headerMap.put("typ", "JWT");


Map<String, Object> claimMap= new HashMap<String, Object>();
claimMap.put("iss", teamId);
claimMap.put("iat", nowTime.getTime());
claimMap.put("exp", expTime);
claimMap.put("aud", appleDomain);
claimMap.put("sub", clientId);

SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.ES256;
byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(getKey());
Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());



String jwt = Jwts.builder()
    .signWith(signatureAlgorithm, signingKey)
    .setClaims(claimMap)
    .setHeader(headerMap).compact();


java.lang.IllegalArgumentException: Elliptic Curve signatures must be computed using an EC PrivateKey.  The specified key of type javax.crypto.spec.SecretKeySpec is not an EC PrivateKey.
at io.jsonwebtoken.impl.crypto.EllipticCurveSigner.<init>(EllipticCurveSigner.java:36)
at io.jsonwebtoken.impl.crypto.DefaultSignerFactory.createSigner(DefaultSignerFactory.java:47)
at io.jsonwebtoken.impl.crypto.DefaultJwtSigner.<init>(DefaultJwtSigner.java:37)
at io.jsonwebtoken.impl.crypto.DefaultJwtSigner.<init>(DefaultJwtSigner.java:32)
at io.jsonwebtoken.impl.DefaultJwtBuilder.createSigner(DefaultJwtBuilder.java:338)
at io.jsonwebtoken.impl.DefaultJwtBuilder.compact(DefaultJwtBuilder.java:320)

如何使用我的 p8 密钥文件使用 EC 私钥(它来自苹果,用于使用 auth api)?

【问题讨论】:

  • 你好SUNA JO,你解决了吗?

标签: java spring jwt sha256


【解决方案1】:

您知道 p8 文件中的密钥类型吗?有不同类型的密钥和不同类型的密码算法。我会说你可能有一个 RSA 密钥对而不是一个 EC 密钥对。

我建议使用keystore explorer 创建一个新密钥并将其添加到密钥库中。

【讨论】:

  • 我是从苹果开发者那里下载的 T.T 你的意思是这个密钥文件不适用于 ES256 吗?
  • 是的。您可以安装密钥库资源管理器并检查您拥有的密钥类型。
猜你喜欢
  • 1970-01-01
  • 2022-07-24
  • 2021-05-31
  • 2019-04-12
  • 2019-09-08
  • 2020-05-17
  • 2016-11-02
  • 2020-03-31
  • 2022-11-30
相关资源
最近更新 更多