我获得了生成SAML的证书。这是我到目前为止所做的,但我在org.apache.xml.security.signature.XMLSignatureException: object not initialized for signature or verification
时收到Signer.signObject(singature)
错误
如果我注释掉这一行并尝试获得SAMLResponse但是没有SignatureValue和DigestValue。但我当然需要它们
有任何想法吗?
(另外我只有证书中的publicKey(),因为这不是由我生成的。如果这首先是正确的,请告诉我。我没有在SO中找到任何相关的回答问题所以张贴在这里。)
doSmthMethod() {
signature = getSignature(); // See below
assertion.setSignature(signature);
try
{
Configuration.getMarshallerFactory().getMarshaller(assertion).marshall(assertion);
Signer.signObject(signature); //**Errors out Here**
} catch (MarshallingException e) {
logger.log(Level.SEVERE,"Marshalling Exception:" + e);
} catch (SignatureException e) {
logger.log(Level.SEVERE,"Signature Exception:"+e);
}
}
...
...
...
...
...
public static Signature getSignature() {
Credential signingCredential = getCredential(); // See below
Signature signature = null;
try {
DefaultBootstrap.bootstrap();
} catch (ConfigurationException e) {
e.printStackTrace();
}
signature = (Signature) Configuration.getBuilderFactory()
.getBuilder(Signature.DEFAULT_ELEMENT_NAME)
.buildObject(Signature.DEFAULT_ELEMENT_NAME);
signature.setSignatureAlgorithm(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1);
signature.setSigningCredential(signingCredential);
SecurityConfiguration secConfig = Configuration.getGlobalSecurityConfiguration();
//Added below for Keyinfo
KeyInfo keyInfo = null;/* (KeyInfo)Configuration.getBuilderFactory()
.getBuilder(Signature.DEFAULT_ELEMENT_NAME)
.buildObject(Signature.DEFAULT_ELEMENT_NAME);*/
NamedKeyInfoGeneratorManager namedKeyInfoGeneratorManager = secConfig.getKeyInfoGeneratorManager();
KeyInfoGeneratorManager keyInfoGeneratorManager = namedKeyInfoGeneratorManager.getDefaultManager();
KeyInfoGeneratorFactory keyInfoGeneratorFactory = keyInfoGeneratorManager.getFactory(signingCredential);
KeyInfoGenerator keyInfoGenerator = keyInfoGeneratorFactory.newInstance();
try{
try {
keyInfo = keyInfoGenerator.generate(signingCredential);
} catch (org.opensaml.xml.security.SecurityException e) {
logger.log(Level.SEVERE,"Exception while creating keyInfo:: "+ e);
}
}catch (SecurityException e) {
logger.log(Level.SEVERE,"Exception occured: "+e);
}
signature.setKeyInfo(keyInfo);
try {
logger.log(Level.INFO,"Prepating signature params...");
System.out.println("In getSignature.....before preparingSignatureParams");
SecurityHelper.prepareSignatureParams(signature, signingCredential, secConfig, null);
System.out.println(" After preparing params");
} catch (SecurityException e) {
logger.log(Level.SEVERE,"Failed to prepareSignature:: "+ e);
System.out.println(" *******Got securirytExcption");
e.printStackTrace();
} catch (org.opensaml.xml.security.SecurityException e) {
logger.log(Level.SEVERE,"Failed to prepareSignature:: "+ e);
System.out.println(" *******Got openSaml securirytExcption");
e.printStackTrace();
}
catch(Exception e)
{
System.out.println(" *******Got Exception in SelfSignassertion");
e.printStackTrace();
}
return signature;
}
private static Credential getCredential()
{
InputStream inStream = new FileInputStream("C:\\samplecert.cer");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert= (X509Certificate)cf.generateCertificate(inStream);
BasicX509Credential credential = new BasicX509Credential();
credential.setEntityCertificate(cert);
credential.setPublicKey(cert.getPublicKey());
return credential;
}