簡體   English   中英

通過OpenSAML中的現有元素創建SAML 2.0斷言

[英]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
}

我遇到兩個錯誤。

  1. DefaultBootstrap.bootstrap(); 使用時,將引發異常java.lang.UnsupportedOperationException: This parser does not support specification "null" version "null"
  2. 刪除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()才能繼續進行編組或反編組。

  1. 在代碼的上一行中, DOM實現已更改為DOOM DocumentBuilderFactoryImpl.setDOOMRequired(true); 即使不贊成使用 ,代碼也正在使用它。 因此,在執行bootstrap()之前,必須將其設置為false因為基礎的JAXB實現使用DOM。

  2. 另外,將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM