我在验证具有分离负载的 JWS 时遇到问题。我基本上复制了 example provided 中的所有步骤在 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;
}
}
最佳答案
尝试将 jws.setPayload(payload);
向下移动到 jws.setCompactSerialization(...);
行之后。
我认为 jws.setCompactSerialization(...);
正在将有效负载覆盖为空字符串,这会破坏签名验证。
关于java - 使用 jose4j 验证具有分离负载的 JWS 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70380691/