我想向 google FCM 发送请求以向浏览器发送推送请求以显示通知。
主要目标是使用带有 SHA256 的 ECDH 算法使用私钥对 JWT 有效负载进行签名,以获取 JWT 令牌。
我尝试使用 ECDH 算法对令牌进行签名,但每次我获得相同有效负载的不同签名时。
这是我的代码的快照
提前致谢
JwtClaims claims = new JwtClaims();
claims.setAudience("https://fcm.googleapis.com");
claims.setExpirationTime(NumericDate.fromSeconds(1560388318));
claims.setSubject("mailto:admin@example.com");
JsonWebSignature jws = new JsonWebSignature();
jws.setHeader("typ", "JWT");
jws.setHeader("alg", "ES256");
jws.setPayload(claims.toJson());
try {
Key key = loadPrivateKey("-kmhPYsH6JKiFjG8C1cS9vx4bCz594yofAwTLa_SOEE");
jws.setKey(key);
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.ECDSA_USING_P256_CURVE_AND_SHA256);
try {
System.out.println(jws.getCompactSerialization());
} catch (JoseException e) {
e.printStackTrace();
}
.
public static PrivateKey loadPrivateKey(String encodedPrivateKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
byte[] decodedPrivateKey = base64Decode(encodedPrivateKey);
// prime256v1 is NIST P-256
ECParameterSpec params = ECNamedCurveTable.getParameterSpec("prime256v1");
ECPrivateKeySpec prvkey = new ECPrivateKeySpec(new BigInteger(decodedPrivateKey), params);
KeyFactory kf = KeyFactory.getInstance("ECDH");
return kf.generatePrivate(prvkey);
}
.
public static byte[] base64Decode(String base64Encoded) {
if (base64Encoded.contains("+") || base64Encoded.contains("/")) {
return BaseEncoding.base64().decode(base64Encoded);
} else {
return BaseEncoding.base64Url().decode(base64Encoded);
}
}
第一次尝试我得到了:
eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJhdWQiOiJodHRwczovL2ZjbS5nb29nbGVhcGlzLmNvbSIsImV4cCI6MTU2MDM4ODMxOCwic3ViIjoibWFpbHRvOmFkbWluQGV4YW1wbGUuY29tIn0.MpGc0pKvXtDb94Ruq5lkQjqCqxFMkVAwzVervnH90RLArvGHUAZ_kO4VcecLhGfIXTCitBKb5M-EKsYR35IT0A
第二次我得到:
eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJhdWQiOiJodHRwczovL2ZjbS5nb29nbGVhcGlzLmNvbSIsImV4cCI6MTU2MDM4ODMxOCwic3ViIjoibWFpbHRvOmFkbWluQGV4YW1wbGUuY29tIn0.qEW0ci1BnXXUUjkfsQkdReznAyIDEPtygxV3B58Sl8v_gTlh8O4HHGzRtxsqdvL5UIJV06e_UJHYPOUDK_MF9A