[英]Creating OpenSAML Assertion from given XML in Java
I have been beating my head against this for a while, and am starting to make progress. 我一直在反对这一点,并开始取得进展。 However, I ran into some trouble converting a string representation of a SAML 2 Assertion (in XML) to an Assertion object. 但是,我遇到了将SAML 2 Assertion(在XML中)的字符串表示形式转换为Assertion对象的问题。
It looks like I am getting a valid org.w3c.dom.Document
with appropriate data, and I seem to be getting a valid SAMLObjectBuilder<Assertion>
from the builder factory, but when I try to put them together all I get is a blank Assertion; 看起来我正在使用适当的数据获得有效的org.w3c.dom.Document
,我似乎从构建器工厂获得了一个有效的SAMLObjectBuilder<Assertion>
,但是当我尝试将它们组合在一起时,我得到的只是一个空白断言; subject, issuer, issue time and so on are all null
, despite them clearly being set in the XML. 主题,发行人,发行时间等都是null
,尽管它们明确地在XML中设置。
Does anyone see what I am doing wrong, and can suggest a solution? 有谁看到我做错了什么,并建议一个解决方案?
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();
At the nameID assignment, assertion.getSubject()
returns null
, failing the remainder of the expression. 在nameID赋值时, assertion.getSubject()
返回null
,使表达式的其余部分失败。
The example I am using is the full XML from sstc-saml-tech-overview-2.0-draft-03, page 10. 我使用的示例是来自sstc-saml-tech-overview-2.0-draft-03,第10页的完整XML。
The function loadXMLFromString()
above is mostly borrowed from In Java, how do I parse XML as a String instead of a file? 上面的函数loadXMLFromString()
主要是从In Java借用的,我如何将XML解析为String而不是文件?
In case someone else is facing the same problem, and runs across this, here is the answer. 如果其他人面临同样的问题,并且遇到这个问题,这就是答案。
https://wiki.shibboleth.net/confluence/display/OpenSAML/OSTwoUsrManJavaCreateFromXML https://wiki.shibboleth.net/confluence/display/OpenSAML/OSTwoUsrManJavaCreateFromXML
Just take the unmarshalling example: 只需采取解组示例:
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);
Then substitute your Document instance for inCommonMDDoc
and look at the result of the final unmarshall()
call. 然后将您的Document实例替换为inCommonMDDoc
并查看最终unmarshall()
调用的结果。 Note that unmarshall()
returns an Object
which you need to cast to the appropriate type. 请注意, unmarshall()
返回一个您需要强制转换为适当类型的Object
。 Hint: you can use use typeof
if you aren't sure what type it is, but watch out for inheritance. 提示:如果你不确定它是什么类型,你可以使用typeof
,但要注意继承。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.