我在验证带有分离负载的 JWS 时遇到问题。我基本上复制了 jose4j 文档中提供的示例中的所有步骤,但由于某种原因,验证仍然返回 false,而它应该成功。
这是我正在使用的代码,使用最新版本的 jose4j。
// signature is the complete JWS in the form: "JOSE Header".."JWS Signature"
// payload is the unencoded JSON string that makes up the request body
public boolean verifySignature(String signature, String payload) {
JsonWebSignature jws = new JsonWebSignature();
jws.setKnownCriticalHeaders(critHeaders); //critical headers from documentation
//Algorithm as provided in documentation
jws.setAlgorithmConstraints(new AlgorithmConstraints(AlgorithmConstraints.ConstraintType.PERMIT,
AlgorithmIdentifiers.ECDSA_USING_P256_CURVE_AND_SHA256));
jws.setPayload(payload);
try {
jws.setCompactSerialization(signature);
String keyId = jws.getKeyIdHeaderValue();
String keyType = jws.getKeyType();
String keyAlg = jws.getAlgorithmHeaderValue();
//Retrieve key from cached jwks
JsonWebKey usedKey = jwks.findJsonWebKey(keyId, keyType, "sig", keyAlg);
jws.setKey(usedKey.getKey());
return jws.verifySignature();
} catch (JoseException e) {
//log
return false;
}
}