Now I am using this code to generate an JWT token(I am using jjwt lib), this is my mini example of code:
package com.dolphin.soa.post;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* @author dolphin
*/
public class MiniExample {
public static void main(String[] args) {
Map<String,Object> jwtHeader = new HashMap<>();
jwtHeader.put("alg","ES256");
jwtHeader.put("kid","YDKL424AF9");
jwtHeader.put("typ","JWT");
Map<String,Object> appleJwtPayload = new HashMap<>();
appleJwtPayload.put("iss","5fb8e836-27d7-4390-8f40-008acd64a29d");
appleJwtPayload.put("iat",System.currentTimeMillis() / 1000L);
appleJwtPayload.put("exp",System.currentTimeMillis() / 1000L + 60 * 15);
appleJwtPayload.put("aud","appstoreconnect-v1");
appleJwtPayload.put("nonce",UUID.randomUUID().toString());
appleJwtPayload.put("bid","com.earth.dolphin");
String appleKey = "-----BEGIN EC PRIVATE KEY-----\n" +
"MHcCAQEEIDxLR4/hZpkIor0r2OlxhG2cVPOjD63jUPFaiRzGomvgoAoGCCqGSM49\n" +
"AwEHoUQDQgAE4B1UkwnabK3TqzeIakdWD3EVdA+IQUlsQFGP/fkLjrbyDxtADpi0\n" +
"JuGLvtKnw/vGEkyAnXethxpebzDCcfdWGQ==\n" +
"-----END EC PRIVATE KEY-----";
SecretKey secretKey = new SecretKeySpec(appleKey.getBytes(), SignatureAlgorithm.ES256.getJcaName());
String accessToken = Jwts.builder()
.setClaims(appleJwtPayload)
.setHeader(jwtHeader)
.signWith(secretKey)
.compact();
System.out.println("Access Token:" + accessToken);
}
}
but when I run this code in the server side, shows this error:
Connected to the target VM, address: '127.0.0.1:57457', transport: 'socket'
Exception in thread "main" io.jsonwebtoken.security.InvalidKeyException: The signing key's algorithm 'SHA256withECDSA' does not equal a valid HmacSHA* algorithm name and cannot be used with HS512.
at io.jsonwebtoken.SignatureAlgorithm.assertValid(SignatureAlgorithm.java:373)
at io.jsonwebtoken.SignatureAlgorithm.assertValidSigningKey(SignatureAlgorithm.java:315)
at io.jsonwebtoken.impl.DefaultJwtBuilder.signWith(DefaultJwtBuilder.java:122)
at io.jsonwebtoken.impl.DefaultJwtBuilder.signWith(DefaultJwtBuilder.java:115)
at com.dolphin.soa.post.MiniExample.main(MiniExample.java:39)
Disconnected from the target VM, address: '127.0.0.1:57457', transport: 'socket'
Process finished with exit code 1
where is going wrong and what should I do to make it work? I have tried to convert the apple p8 file to generate the private key?
openssl ec -in AuthKey_Y24AF9.p8 -out au_private.p8
still not fix this problem. I have struggled in the problem for a while and searching from internet but still no clue.