从Java中的给定XML创建OpenSAML断言

时间:2011-01-12 10:47:43

标签: java xml saml opensaml

我一直在反对这一点,并开始取得进展。但是,我在将SAML 2 Assertion(在XML中)的字符串表示转换为Assertion对象时遇到了一些麻烦。

看起来我得到了一个有效org.w3c.dom.Document的合适数据,而且我似乎从构建工厂获得了一个有效的SAMLObjectBuilder<Assertion>,但当我尝试将它们组合在一起时,我得到的只是一个空白的断言;主题,发行人,发行时间等都是null,尽管它们明确地在XML中设置。

有人看到我做错了什么,可以提出解决方案吗?

Document doc = loadXMLFromString(saml);

XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();

SAMLObjectBuilder<Assertion> assertionBuilder =
  (SAMLObjectBuilder<Assertion>)
  builderFactory.getBuilder(Assertion.DEFAULT_ELEMENT_NAME);

Assertion assertion = assertionBuilder.buildObject(doc.getDocumentElement());

String nameID = assertion.getSubject().getNameID().getValue();

在nameID分配时,assertion.getSubject()返回null,表达式的其余部分失败。

我使用的示例是来自sstc-saml-tech-overview-2.0-draft-03,第10页的完整XML。

上面的函数loadXMLFromString()主要来自In Java, how do I parse XML as a String instead of a file?

1 个答案:

答案 0 :(得分:9)

如果其他人遇到同样的问题,并遇到这个问题,这就是答案。

https://wiki.shibboleth.net/confluence/display/OpenSAML/OSTwoUsrManJavaCreateFromXML

请采取解组示例:

String inCommonMDFile = "/data/org/opensaml/saml2/metadata/InCommon-metadata.xml";

// Initialize the library
DefaultBootstrap.bootstrap(); 

// Get parser pool manager
BasicParserPool ppMgr = new BasicParserPool();
ppMgr.setNamespaceAware(true);

// Parse metadata file
InputStream in = MetadataTest.class.getResourceAsStream(inCommonMDFile);
Document inCommonMDDoc = ppMgr.parse(in);
Element metadataRoot = inCommonMDDoc.getDocumentElement();

// Get apropriate unmarshaller
UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataRoot);

// Unmarshall using the document root element, an EntitiesDescriptor in this case
EntitiesDescriptor inCommonMD = (EntitiesDescriptor) unmarshaller.unmarshall(metadataRoot);

然后将您的Document实例替换为inCommonMDDoc,并查看最终unmarshall()调用的结果。请注意,unmarshall()会返回Object,您需要将其转换为相应的类型。提示:如果你不确定它是什么类型,你可以使用typeof,但要注意继承。