OpenSAML自定义属性值

时间:2011-08-01 14:59:41

标签: java saml opensaml

我正在尝试创建SAML响应。构成断言的其中一个属性称为地址,属性值需要是在XSD中定义的自定义类型。如何向响应中添加自定义属性值类型?

2 个答案:

答案 0 :(得分:4)

如果您的属性值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);

答案 1 :(得分:0)

实际上,上面的内容并没有正确的结果。以上示例仅可用于创建具有文本内容而非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; 
}