a C*_*CVn 9 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();
Run Code Online (Sandbox Code Playgroud)
在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);
Run Code Online (Sandbox Code Playgroud)
然后替换您的Document实例inCommonMDDoc并查看最终unmarshall()调用的结果.请注意,unmarshall()返回Object需要强制转换为适当类型的内容.提示:typeof如果你不确定它是什么类型,你可以使用,但要注意继承.