首页 > 解决方案 > Spring Boot 项目上的 LDAP 和 SSO 身份验证

问题描述

我目前正在开发一个新项目(来自 scracth),该项目从带有 Spring Security 的 Spring Boot 开始。

我需要在同一个 REST API 上实现两种身份验证方式。首先是 SSO 身份验证和 LDAP 身份验证,用户通过单击将身份验证请求传输到 API 的 Web 应用程序上的复选框来进行选择。

我的问题是:我怎样才能做到这一点?我已经实现了 LDAP 身份验证或 SSO 身份验证,但从来没有在同一个项目中实现,我没有找到任何文档

问候

标签: springspring-bootspring-securityspring-security-oauth2spring-security-ldap

解决方案


似乎您需要实现自己的AuthenticationProvider. 请参见下面的代码:

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

@Override
public Authentication authenticate(Authentication authentication) 
  throws AuthenticationException {

    String name = authentication.getName();
    String password = authentication.getCredentials().toString();

    if (shouldAuthenticateAgainstThirdPartySystem()) {

        // use the credentials
        // and authenticate against the third-party system
        return new UsernamePasswordAuthenticationToken(
          name, password, new ArrayList<>());
    } else {
        return null;
    }
}

@Override
public boolean supports(Class<?> authentication) {
    return authentication.equals(
      UsernamePasswordAuthenticationToken.class);
}
}

代码来自:http ://www.baeldung.com/spring-security-authentication-provider

shouldAuthenticateAgainstThirdPartySystem您可以检查请求(https://stackoverflow.com/a/26323545/878361 并决定使用 ldap 或 sso。


推荐阅读