在 Java 中根据给定 XML 创建 OpenSAML 断言
我已经为此努力了一段时间,并且开始取得进展。但是,我在将 SAML 2 断言(XML 格式)的字符串表示形式转换为断言对象时遇到了一些麻烦。
看起来我正在获取带有适当数据的有效 org.w3c.dom.Document
,并且我似乎从构建器工厂获取了有效的 SAMLObjectBuilder
,但是当我尝试将它们放在一起时,我得到的只是一个空白的断言;主题、发行者、发行时间等都为 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()
大部分是从 在Java中,我该如何将 XML 解析为字符串而不是文件?
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.
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; subject, issuer, issue time and so on are all null
, despite them clearly being set in the 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.
The example I am using is the full XML from sstc-saml-tech-overview-2.0-draft-03, page 10.
The function loadXMLFromString()
above is mostly borrowed from In Java, how do I parse XML as a String instead of a file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果其他人也面临同样的问题并遇到这个问题,这里就是答案。
https://wiki.shibboleth.net/confluence/display/OpenSAML/OSTwoUsrManJavaCreateFromXML
以解组示例为例:
然后用您的 Document 实例替换
inCommonMDDoc
并查看最终unmarshall()
调用的结果。请注意,unmarshall()
返回一个Object
,您需要将其转换为适当的类型。提示:如果您不确定它是什么类型,可以使用typeof
,但要注意继承。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
Just take the unmarshalling example:
Then substitute your Document instance for
inCommonMDDoc
and look at the result of the finalunmarshall()
call. Note thatunmarshall()
returns anObject
which you need to cast to the appropriate type. Hint: you can use usetypeof
if you aren't sure what type it is, but watch out for inheritance.