[英]How to create a valid SAML 2.0 Assertion with OpenSAML library in Java
[英]Create SAML 2.0 assertion by existing element in OpenSAML
我正在尝试使用现有的断言元素为令牌续订过程创建带有OpenSAML的SAML 2.0断言。
// Obtain the token
Token tk = tkStorage.getToken(data.getTokenId());
OMElement assertionOMElement = tk.getToken();
int samlRstversion = data.getSamlRstVersion();
if(samlRstversion == 2) {
DefaultBootstrap.bootstrap();
UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller((Element)assertionOMElement);
Element x1 = (Element)assertionOMElement;
Assertion samlAssertion = (Assertion) unmarshaller
.unmarshall(x1);
//Add conditions to the assertion
}
我遇到两个错误。
DefaultBootstrap.bootstrap();
使用时,将引发异常java.lang.UnsupportedOperationException: This parser does not support specification "null" version "null"
DefaultBootstrap.bootstrap()
,它将抛出Assertion samlAssertion = (Assertion) unmarshaller.unmarshall(x1);
有什么我想念的吗?
首先,您始终必须运行引导程序,否则会出错。
似乎第一个错误是因为您的JAXP实现太旧了https://lists.internet2.edu/sympa/arc/mace-opensaml-users/2010-01/msg00015.html
OpenSAML团队建议使用Apache Xerces或Xalan。
有两个导致异常的错误。 当然,必须执行bootsrap()
才能继续进行编组或反编组。
在代码的上一行中, DOM
实现已更改为DOOM
。 DocumentBuilderFactoryImpl.setDOOMRequired(true);
即使不赞成使用 ,代码也正在使用它。 因此,在执行bootstrap()
之前,必须将其设置为false
因为基础的JAXB实现使用DOM。
另外,将OMElement assertionOMElement
强制转换为Element
引发此异常。 org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
解决方案是将OMElement
转换为String
,然后从中构建Document
并获取DocumentElement
String s = assertionOMElement.toString();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new ByteArrayInputStream(s.trim().getBytes()));
Element element = document.getDocumentElement();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.