spring-boot saml2login在AuthenticationSuccessHandler中获取依赖方实体I



我有一个多原则应用程序,它使用spring-boot saml2login(最终从spring-saml扩展迁移而来(。两个承租人可以对一个用户(通过电子邮件地址识别(进行身份验证。想想两个学区,小学和高中都有一个家长账户(parent@gmail.com)他们在每栋楼都有两个孩子。我需要知道哪个IDP正在向我发送saml响应。在老派,春假延期,我可以做:

final SAMLCredential samlCred = (SAMLCredential) (authentication.getCredentials());
if (samlCred != null) {
return getCustomerService().getActiveCustomerBySamlEntityId(samlCred.getRemoteEntityID())
.filter(c -> c != null && c.isFullyActive());
}

要获取实体ID和关联的客户,但在springboot 2.5+中使用新的saml2login,请调用:

authentication.getCredentials() 

只是返回一个SAML响应(XML(字符串,我需要解析(也许还需要解密(。那么,有没有一种简单的方法可以获得IPD的信息,最好是来自Spring Security中的Authentication(或Saml2AuthenticatedPrincipal(的实体ID?

以下是我如何做到这一点的,以防其他人好奇:

获取原始xml响应:

Saml2Authentication.getSaml2Response();

将XML传递到以下实用程序函数中:

public static Optional<String> parseIdpEntityId(String xmlResponse) {
if ( xmlResponse == null || xmlResponse.trim().length() == 0 ) {
log.info("Failed to parse XML SAML Response, null or empty.");
return Optional.empty();
}
try (InputStream inputStream = new ByteArrayInputStream(xmlResponse.getBytes())) {
Document messageDoc;
BasicParserPool basicParserPool = new BasicParserPool();
basicParserPool.initialize();
messageDoc = basicParserPool.parse(inputStream);
Element messageElem = messageDoc.getDocumentElement();
Unmarshaller unmarshaller = XMLObjectProviderRegistrySupport.getUnmarshallerFactory().getUnmarshaller(messageElem);
XMLObject samlObject = unmarshaller.unmarshall(messageElem);
Response response = (Response) samlObject;
if ( response != null && response.getIssuer() != null ) {
return Optional.ofNullable(response.getIssuer().getValue().trim());
}
} catch (Exception e) {
log.warn("Failed to parse SAML response: {}", e.getMessage(), e);
}
return Optional.empty();
}

最新更新