OpenSAML 自定义属性值

发布于 2024-11-27 11:04:48 字数 79 浏览 0 评论 0原文

我正在尝试创建 SAML 响应。组成断言的属性之一称为地址,并且属性值需要是在 XSD 中定义的自定义类型。如何将自定义属性值类型添加到响应中?

I'm trying to create a SAML response. One of the attributes that makes up the assertion is called address and the attribute value needs to be a custom type that is defined in an XSD. How do I add custom attribute value types to the response?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

嗳卜坏 2024-12-04 11:04:48

如果您的属性值 XML 为字符串形式:


String yourXMLFragment = "...";

AttributeStatementBuilder attributeStatementBuilder = 
   (AttributeStatementBuilder) builderFactory.getBuilder(AttributeStatement.DEFAULT_ELEMENT_NAME);
AttributeStatement attributeStatement = attributeStatementBuilder.buildObject();

AttributeBuilder attributeBuilder = 
   (AttributeBuilder) builderFactory.getBuilder(Attribute.DEFAULT_ELEMENT_NAME);
Attribute attr = attributeBuilder.buildObject();
attr.setName("yourAttributeName");

XSAnyBuilder sb2 = (XSAnyBuilder) builderFactory.getBuilder(XSAny.TYPE_NAME);
XSAny attrAny = sb2.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSAny.TYPE_NAME);
attrAny.setTextContent(yourXMLFragment.trim());

attr.getAttributeValues().add(attrAny);
attributeStatement.getAttributes().add(attr);

If your attribute value XML is in String form:


String yourXMLFragment = "...";

AttributeStatementBuilder attributeStatementBuilder = 
   (AttributeStatementBuilder) builderFactory.getBuilder(AttributeStatement.DEFAULT_ELEMENT_NAME);
AttributeStatement attributeStatement = attributeStatementBuilder.buildObject();

AttributeBuilder attributeBuilder = 
   (AttributeBuilder) builderFactory.getBuilder(Attribute.DEFAULT_ELEMENT_NAME);
Attribute attr = attributeBuilder.buildObject();
attr.setName("yourAttributeName");

XSAnyBuilder sb2 = (XSAnyBuilder) builderFactory.getBuilder(XSAny.TYPE_NAME);
XSAny attrAny = sb2.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSAny.TYPE_NAME);
attrAny.setTextContent(yourXMLFragment.trim());

attr.getAttributeValues().add(attrAny);
attributeStatement.getAttributes().add(attr);
幸福%小乖 2024-12-04 11:04:48

实际上上面的方法并没有产生正确的结果。上面的示例只能用于创建具有文本内容而不是 xml 内容的 xsany(xml 内容被转义)。

因此,在深入研究 opensaml 源代码之后,以下内容确实按需要工作了:

public XSAny createXSAny(Element dom)
{
XSAnyBuilder anyBuilder = (XSAnyBuilder) Configuration.getBuilderFactory().getBuilder(XSAny.TYPE_NAME);
    XSAny any = anyBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSAny.TYPE_NAME);
    // this builds only the root element not the whole dom
    XSAny xo=anyBuilder.buildObject(dom);
    // set/populate dom so whole dom gets into picture
    xo.setDOM(dom);
    any.getUnknownXMLObjects().add(xo);

  return any; 
}

Actually this above does not yeld correct results. The above example can be used only to create xsany with text content not xml content (xml content gets escaped).

So after digging in opensaml sources the following did work as needed:

public XSAny createXSAny(Element dom)
{
XSAnyBuilder anyBuilder = (XSAnyBuilder) Configuration.getBuilderFactory().getBuilder(XSAny.TYPE_NAME);
    XSAny any = anyBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSAny.TYPE_NAME);
    // this builds only the root element not the whole dom
    XSAny xo=anyBuilder.buildObject(dom);
    // set/populate dom so whole dom gets into picture
    xo.setDOM(dom);
    any.getUnknownXMLObjects().add(xo);

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