2 回答
TA贡献2037条经验 获得超6个赞
//remember to remove two lines below in your .p8 key before run code
//-----BEGIN PRIVATE KEY-----
//-----END PRIVATE KEY-----
//begin create a key from .p8 file
byte[] p8der = Files.readAllBytes(new File("/tmp/AuthKey_KeyId.p8").toPath());
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(new org.apache.commons.codec.binary.Base64().decode(p8der));
PrivateKey appleKey = KeyFactory.getInstance("EC").generatePrivate(priPKCS8);
//end create a key from .p8 file
Map jwtHeader = new HashMap();
jwtHeader.put("alg", "ES256");
//addmore header
JsonObject jwtPayload = new JsonObject();
jwtPayload.addProperty("iss", "{{replace with your issuer ID}}");
//addmore properties
JsonArray scope = new JsonArray();
scope.add("GET {{replace with your scope}}");
jwtPayload.add("scope", scope);
//begin signWithES256
JwtBuilder jwtBuilder = Jwts.builder()
.setHeader(jwtHeader)
.setPayload(jwtPayload.toString())
.signWith(
SignatureAlgorithm.ES256,
appleKey
);
String jws = jwtBuilder.compact();
logger.info(jws);
添加回答
举报