繁体   English   中英

如何使用opensaml验证(azure)saml xml响应?

[英]How to validate (azure) saml xml response with opensaml?

我从我的IDP获得XML格式的响应,并想使用opensaml2对其进行验证。 如何做呢?

根据OpenSAML2官方文档( doc1doc2 ),您可以尝试使用下面的代码通过OpenSAML验证saml xml响应。

// Initialize the library
DefaultBootstrap.bootstrap(); 

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

// Get org.w3c.dom.Document Object from response
HttpURLConnection req = (HttpURLConnection) new URL("<saml-xml-url>").openConnection();
// Add some necessary headers for the request
// req.addRequestProperty("...", "...");
// ...
InputStream in = req.getInputStream();
Document inCommonMDDoc = ppMgr.parse(in);

// Get the DOMSource from org.w3c.dom.Document Object
DOMSource domSource=new DOMSource(document);  

//Add an extension schema via the code SAMLSchemaBuilder.addExtensionSchema(String schema) if necessary
Schema schema = SAMLSchemaBuilder.getSAML11Schema();

// Get a Validator instance.
Validator validator = schema.newValidator();
try {
    validator.validate(domSource);
    System.out.println("Result : Valid!");
} catch(Exception e) {
    System.out.println("Result : Invalid!");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM