![](/img/trans.png)
[英]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.