在 Java 中根据给定 XML 创建 OpenSAML 断言

发布于 2024-10-12 01:46:05 字数 1135 浏览 4 评论 0原文

我已经为此努力了一段时间,并且开始取得进展。但是,我在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2024-10-19 01:46:05

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

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

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:

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. Note that unmarshall() returns an Object which you need to cast to the appropriate type. Hint: you can use use typeof if you aren't sure what type it is, but watch out for inheritance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文