Java 签名加密验证未成功opensaml
我正在尝试对SAML2响应进行签名验证,该响应是使用OpenSAML从身份提供者处获得的。我正在尝试从本地文件系统读取响应 这是我的密码:Java 签名加密验证未成功opensaml,java,validation,saml,saml-2.0,opensaml,Java,Validation,Saml,Saml 2.0,Opensaml,我正在尝试对SAML2响应进行签名验证,该响应是使用OpenSAML从身份提供者处获得的。我正在尝试从本地文件系统读取响应 这是我的密码: DefaultBootstrap.bootstrap(); BasicParserPool ppMgr = new BasicParserPool(); ppMgr.setNamespaceAware(true); //Read file from the filesystem File file1=new File("F:/Softwa
DefaultBootstrap.bootstrap();
BasicParserPool ppMgr = new BasicParserPool();
ppMgr.setNamespaceAware(true);
//Read file from the filesystem
File file1=new File("F:/Softwares/Assertion.xml");
InputStream inCommonSaml=new FileInputStream(file1);
// Parse file
Document inCommonSamlDoc = ppMgr.parse(inCommonSaml);
Element metadataRoot = inCommonSamlDoc.getDocumentElement();
UnmarshallerFactory unmarshallerFactory=configuration.getUnmarshallerFactory();
Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataRoot);
Response inCommonSamlRes = (Response) unmarshaller.unmarshall(metadataRoot);
//Get certificate
SignatureValidator signatureValidator = new SignatureValidator(cert);
Signature signature=inCommonSamlRes.getSignature();
signatureValidator.validate(signature);
try {
BasicX509Credential credential = new BasicX509Credential();
File file2=new File("F:/Softwares/publicKey.crt");
InputStream samlCertificate=new FileInputStream(file2);
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
//
@SuppressWarnings("deprecation")
java.security.cert.X509Certificate certificate = (java.security.cert.X509Certificate) certificateFactory.generateCertificate(samlCertificate);
//
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec((certificate).getPublicKey().getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey key = keyFactory.generatePublic(publicKeySpec);
credential.setPublicKey(key);
Object obj = (credential).getPublicKey();
if (obj instanceof RSAPublicKey) {
BigInteger modulus = ((RSAPublicKey) obj).getModulus();
BigInteger exponent = ((RSAPublicKey) obj).getPublicExponent();
System.out.println("modulus");
System.out.println (org.apache.commons.codec.binary.Base64.encodeBase64String(modulus.toByteArray()));
System.out.println("public exponent:");
System.out.println (org.apache.commons.codec.binary.Base64.encodeBase64String(exponent.toByteArray()));
}
// System.out.println ("public key is: //n//r"+ credential.getPublicKey());
return credential;
} catch (Exception e) {
throw e; //Throws a 'Signature did not validate against the credential's key' exception
}
注意:我也使用相同的证书(publicKey.crt)对断言进行签名。我得到以下错误: 签名加密验证未成功 请让我知道我哪里错了?这个错误意味着什么?它是否说公钥和私钥是相同的 谢谢,
aswini J我认为您需要从IdP(身份提供者)服务器获取.jks文件。此外,当您从IDP获取帖子的SAMLResponse时,该SAMLResponse应该包含签名(仅供参考-将是一个编码字符串,您可以使用Maven中央存储库上提供的开放SAML库进行解码和读取)。 一旦您从中获得签名,就可以使用OpenSAML验证该签名
sigValidator.validate(response.getSignature());
如果一切正常,这个方法会给你带来惊喜。参考信息“使用提供的凭证中的密钥验证签名”
您可以点击以下链接:获取签名和
(IMP):您的IDP(身份提供者)应该被发送(可以通过HTTP POST),这样您就可以从中获得NameID,即ClinetId此答案适用于收到此错误并在谷歌上搜索的新用户 在我的例子中,我面临着相同的错误:“错误:签名加密验证未成功”,情况略有不同。我使用SimpleSamlPHP作为SP进行验证,使用CAS作为IDP进行验证 问题是,我的SP使用一个(自签名)证书对IDP进行SP验证,并在Apache中使用另一个SP证书来允许SSL(这样我就可以使用https) 使用两个单独的证书可能适用于许多过程,如登录和元数据验证,但使用过程,如注销,将发生上述错误
解决方案是两个进程只使用一个证书,我的错误就消失了 你能在gist?.jks上发布assertion.xml吗?你是说java密钥吗?如何得到它?我使用一个基于java的工具作为身份提供者。ThanksNo,它是由keytool创建的。您正在使用哪个IDP(身份提供者)?在我的应用程序中,我使用的是WSO2 IDP,它有wso2carbon.jks文件。此文件包含公钥和私钥…私钥与我在一起,公钥与服务器在一起,它们是用于标识彼此身份的密钥对,