【问题标题】:Spring Security: Authenticate against multiple LDAP servers & DAO-based authenticationSpring Security:针对多个 LDAP 服务器和基于 DAO 的身份验证进行身份验证
【发布时间】:2016-03-03 11:05:36
【问题描述】:

我正在开发一个 Springboot 应用程序,该应用程序需要支持本地身份验证(通过基于 DAO 的提供程序)和多个 LDAP 服务器(管理配置,存储在数据库中)。

使用单个 LDAP 提供程序,我的配置方法如下所示:

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(daoAuthenticationProvider());
auth.ldapAuthentication()
        .userSearchBase(userSearchBase)
        .groupSearchBase(groupSearchBase)
        .userSearchFilter(userSearchFilter)
        .userDetailsContextMapper(new DaoUserDetailsContextMapper())
        .contextSource().url(url+"/"+base)
        .managerPassword(managerPassword)
        .managerDn(managerDn);
  }

通过其他类似的帖子,这似乎可以通过创建多个 LDAP 提供者来完成,并且 Spring 安全性将循环遍历每个提供者,直到找到成功的登录。我将关联的 LDAP 配置记录关联为 User 表上的外键。

是否有更有效的方法来尝试与用户关联的特定 LDAP 端点,或者最好让 Spring 遍历可用的提供程序?

感谢您的任何意见!

【问题讨论】:

  • 您好,您解决了吗?我有类似的任务。

标签: spring spring-mvc authentication spring-security spring-ldap


【解决方案1】:

经过长时间的搜索,我发现了一些关于 Spring Security 身份验证如何工作的有趣内容(有一个视频:https://youtu.be/caCJAJC41Rk?t=781

之后,您可以使用 Spring 实现的系统,即覆盖 supports(class<?> authenticationClass) 方法。此方法用作“您,AuthenticationProvider,可以管理这种 AuthenticationClass 吗?”如果为 true,则提供者将尝试对用户进行身份验证,如果不是,则从不执行任何任务。

这样,您可以实现自己的CustomAuthentificationProvider,它实现了AuthenticationProvider 接口。

public class LocalUserAuthenticationProvider implements AuthenticationProvider {

    private final UserRepository;

    public LocalUserAuthenticationProvider(UserRepository userRepository){
        this.userRepository = userRepository;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        /*my jobs*/
        throws new AuthenticationException("Cannot authenticate");
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return MyUsernameAuthenticationToken.class.isAssignableFrom(aClass);
    }
}

使用您自己的 AuthenticationToken

public class StringUsernameAuthenticationToken extends UsernamePasswordAuthenticationToken {

    public StringUsernameAuthenticationToken(Object principal, Object credentials) {
        super(principal, credentials);
    }

    public StringUsernameAuthenticationToken(Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities) {
        super(principal, credentials, authorities);
    }
}

实际上我在 Spring Security 实现的 AuthenticationManagerBuilder 中没有找到任何具有 LDAP 身份验证的解决方案(参考:https://spring.io/guides/gs/authenticating-ldap/

希望这可以帮助人们

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-18
    • 2013-01-11
    • 2015-07-22
    • 2020-11-09
    • 2011-02-01
    • 2018-08-09
    • 2016-09-01
    • 2011-08-16
    相关资源
    最近更新 更多