繁体   English   中英

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

[英]Creating OpenSAML Assertion from given XML in Java

我一直在反对这一点,并开始取得进展。 但是,我遇到了将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借用的,我如何将XML解析为String而不是文件?

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

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 ,但要注意继承。

暂无
暂无

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

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