从Java中给定XML创建OpenSAML断言
题
一段时间以来,我一直在殴打我的头,并开始取得进步。但是,我遇到了一些麻烦,因为将SAML 2断言(XML)的字符串表示转换为断言对象。
看起来我有效 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();
在名称分配中, assertion.getSubject()
返回 null
, ,未能表达其余部分。
我正在使用的示例是SSTC-SAML-TECH-OVERVIEW-2.0-DRAFT-03,第10页的完整XML。
功能 loadXMLFromString()
以上主要是从中借来的 在Java中,如何将XML解析为字符串而不是文件?
解决方案
如果其他人正面临着同样的问题,并且可以解决这个问题,这就是答案。
https://wiki.shibboleth.net/confluence/display/opensaml/ostwousrmanjavacrotefromxml
只需举一个Unmarshalling示例:
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);
然后将您的文档实例替换为 inCommonMDDoc
看看决赛的结果 unmarshall()
称呼。注意 unmarshall()
返回 Object
您需要施放适当的类型。提示:您可以使用 typeof
如果您不确定它是哪种类型,但请注意继承。
不隶属于 StackOverflow