简体   繁体   English

如何使用开放式SAML签名SAML响应?

[英]How to Sign SAML Response using Open SAML?

I have to implement a similar logic to this in Java. 我要实现类似的逻辑, 在Java中。

I have an XML which is not signed and the message before sending to Service provider. 发送给服务提供商之前,我有未签名的XML和消息。 I have both private and public key and unsigned XML. 我有私钥和公钥以及未签名的XML。

Can some one help me providing a code snippet exactly which method to called in order to sign the message using public and private key. 有人可以帮我提供一个代码段,确切地称为要使用公钥和私钥对消息进行签名的方法。

You can also use onelogin saml-java utils from Onelogin Saml Java - that one seems to be much easier to use (they have method to load public, private key, document from string, etc. Then you can use it to sign either the whole SAML response or to sign assertion and then the response: 您还可以使用Onelogin Saml Java中的 onelogin saml-java utils-似乎更易于使用(它们具有从字符串等中加载公钥,私钥,文档等的方法。然后,您可以使用它来对整个签名SAML响应或签名断言,然后响应:

Document document = Util.loadXML(saml); //loads string to document

//load private key and certificate
X509Certificate cert = Util.loadCert(pubKeyBytes);
PrivateKey privateKey = Util.loadPrivateKey(privKeyBytes);

//sign the response
String signedResponse = Util.addSign(document, privateKey, cert, null);

to use this library, just add 要使用此库,只需添加

<dependency>
    <groupId>com.onelogin</groupId>
    <artifactId>java-saml</artifactId>
    <version>2.0.0</version>
</dependency>

dependency to your project's pom.xml 对项目的pom.xml的依赖

You create a Signature object and set the signing properties on it 您创建一个签名对象并在其上设置签名属性

signature.setSigningCredential(credential);
signature.setSignatureAlgorithm(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1);
signature.setCanonicalizationAlgorithm(SignatureConstants.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);

Then you set the signature object on the Response 然后在响应上设置签名对象

.setSignature(signature)

Next you marshall the response object 接下来,将响应对象编组

XMLObjectProviderRegistrySupport
   .getMarshallerFactory()
   .getMarshaller(response)
   .marshall(response);

Lastly you use the Signer class to perform the signing 最后,您使用Signer类执行签名

Signer.signObject(signature);

I have more information in this post on my blog 我在博客上的这篇文章中有更多信息

对于opensaml3,请使用:

import org.opensaml.xmlsec.signature.support.Signer;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM