我无法使用 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)?