簡體   English   中英

從Java中的給定XML創建OpenSAML斷言

[英]Creating OpenSAML Assertion from given XML in Java

我一直在反對這一點,並開始取得進展。 但是,我遇到了將SAML 2 Assertion(在XML中)的字符串表示形式轉換為Assertion對象的問題。

看起來我正在使用適當的數據獲得有效的org.w3c.dom.Document ,我似乎從構建器工廠獲得了一個有效的SAMLObjectBuilder<Assertion> ,但是當我嘗試將它們組合在一起時,我得到的只是一個空白斷言; 主題,發行人,發行時間等都是null ,盡管它們明確地在XML中設置。

有誰看到我做錯了什么,並建議一個解決方案?

Document doc = loadXMLFromString(saml);

XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();

SAMLObjectBuilder<Assertion> assertionBuilder =
  (SAMLObjectBuilder<Assertion>)
  builderFactory.getBuilder(Assertion.DEFAULT_ELEMENT_NAME);

Assertion assertion = assertionBuilder.buildObject(doc.getDocumentElement());

String nameID = assertion.getSubject().getNameID().getValue();

在nameID賦值時, assertion.getSubject()返回null ,使表達式的其余部分失敗。

我使用的示例是來自sstc-saml-tech-overview-2.0-draft-03,第10頁的完整XML。

上面的函數loadXMLFromString()主要是從In Java借用的,我如何將XML解析為String而不是文件?

如果其他人面臨同樣的問題,並且遇到這個問題,這就是答案。

https://wiki.shibboleth.net/confluence/display/OpenSAML/OSTwoUsrManJavaCreateFromXML

只需采取解組示例:

String inCommonMDFile = "/data/org/opensaml/saml2/metadata/InCommon-metadata.xml";

// Initialize the library
DefaultBootstrap.bootstrap(); 

// Get parser pool manager
BasicParserPool ppMgr = new BasicParserPool();
ppMgr.setNamespaceAware(true);

// Parse metadata file
InputStream in = MetadataTest.class.getResourceAsStream(inCommonMDFile);
Document inCommonMDDoc = ppMgr.parse(in);
Element metadataRoot = inCommonMDDoc.getDocumentElement();

// Get apropriate unmarshaller
UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataRoot);

// Unmarshall using the document root element, an EntitiesDescriptor in this case
EntitiesDescriptor inCommonMD = (EntitiesDescriptor) unmarshaller.unmarshall(metadataRoot);

然后將您的Document實例替換為inCommonMDDoc並查看最終unmarshall()調用的結果。 請注意, unmarshall()返回一個您需要強制轉換為適當類型的Object 提示:如果你不確定它是什么類型,你可以使用typeof ,但要注意繼承。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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