Java 通过OpenSAML中的现有元素创建SAML2.0断言

Java 通过OpenSAML中的现有元素创建SAML2.0断言,java,opensaml,ws-trust,Java,Opensaml,Ws Trust,我正在尝试使用现有的断言元素为令牌更新过程使用OpenSAML创建SAML2.0断言 // Obtain the token Token tk = tkStorage.getToken(data.getTokenId()); OMElement assertionOMElement = tk.getToken(); int samlRstversion = data.getSamlRstVersion(); if(sa

我正在尝试使用现有的断言元素为令牌更新过程使用OpenSAML创建SAML2.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:此解析器不支持规范“null”版本“null”
  • 当删除
    DefaultBootstrap.bootstrap()
    时,它会抛出 断言samlAssertion=
    (断言)解组器。解组器(x1)

  • 有什么我遗漏的吗?

    首先,您必须始终运行引导程序,否则会出现错误

    第一个错误是因为JAXP的实现太旧了


    OpenSAML团队建议使用ApacheXerces或Xalan。

    有两个错误导致了异常。当然,为了继续编组或解编组,必须执行
    bootsrap()

  • 在代码的前一行中,
    DOM
    实现正在更改为
    DOOM
    DocumentBuilderFactoryImpl.setDOOMRequired(true)
    即使是这样,代码也在使用它。因此,在执行
    bootstrap()
    之前,必须将其设置为
    false
    ,因为底层JAXB实现使用DOM

  • 另外,将
    OmeElement AssertionMelement
    强制转换为
    元素
    会引发此异常
    org.w3c.dom.DOMException:NAMESPACE\u ERR:试图以不正确的方式创建或更改对象。

  • 解决方案是将
    omeelement
    转换为
    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();