java-samlJava 的 SAML 开发包
java-saml 是 Java 的 SAML 开发包。
Maven:
<dependency> <groupId>com.onelogin</groupId> <artifactId>java-saml</artifactId> <version>2.4.0</version> </dependency>
示例代码:
Map<String, Object> samlData = new HashMap<>(); samlData.put("onelogin.saml2.sp.entityid", "http://localhost:8080/java-saml-tookit-jspsample/metadata.jsp"); samlData.put("onelogin.saml2.sp.assertion_consumer_service.url", new URL("http://localhost:8080/java-saml-tookit-jspsample/acs.jsp")); samlData.put("onelogin.saml2.security.want_xml_validation",true); samlData.put("onelogin.saml2.sp.x509cert", myX509CertInstance); SettingsBuilder builder = new SettingsBuilder(); Saml2Settings settings = builder.fromValues(samlData).build(); Auth auth = new Auth(settings, request, response); String targetUrl = 'https://example.com'; auth.login(returnTo=targetUrl) auth.getLastRequestId() Auth auth = new Auth(); Saml2Settings settings = auth.getSettings(); String metadata = settings.getSPMetadata(); List<String> errors = Saml2Settings.validateMetadata(metadata); if (errors.isEmpty()) { out.println(metadata); } else { response.setContentType("text/html; charset=UTF-8"); for (String error : errors) { out.println("<p>"+error+"</p>"); } }
评论