【问题标题】:Spring Boot security - multiple authentication providersSpring Boot 安全性 - 多个身份验证提供程序
【发布时间】:2015-04-10 16:09:13
【问题描述】:

我的问题是我想要两个身份验证提供程序

之前: 我有我的 UserDetailServiceImpl 并且我针对 DB 验证了用户(不确定它是什么提供者)用户根据 DB 中的数据获得了角色

现在: 我使用 ActiveDirectoryLdapAuthentication 提供程序如下

  @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        super.configure(auth);
        auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailService);
    }

    @Bean
    public AuthenticationManager authenticationManager() {
        return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
    }
    @Bean
    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
       MyActiveDirectoryLdapAuthenticationProvider provider = new MyActiveDirectoryLdapAuthenticationProvider(domain, url, rootDN);
        provider.setConvertSubErrorCodesToExceptions(true);
        provider.setUseAuthenticationRequestCredentials(true);

        return provider;
    }

我让它工作了,所以我能够进行身份验证。

问题:

  1. 我现在无法使用数据库用户登录,现在只能使用 LDAP。
  2. 没有使用UserDetailsS​​ervice,那么用户有什么角色呢?
  3. 有没有办法向 LDAP 认证用户添加一些默认角色?

那么如何启用这两个提供者呢? 如何向用户添加角色,通过 LDAP auth.provider 进行身份验证?

我也非常感谢对这里发生的事情的“大图”描述(引擎盖下)。这里使用的每个类的作用是什么,真的不清楚它是如何工作的(AuthenticationManager、AuthenticationManagerBuilder、AuthenticationProvider 等)。也许它只是被自动配置隐藏了等等,但即使直接看 Spring Security 也不会让我更聪明。

感谢任何提示

更新 这段代码似乎工作正常

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailService);
    }

    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
        MyActiveDirectoryLdapAuthenticationProvider provider = new MyActiveDirectoryLdapAuthenticationProvider(domain, url, rootDN);
        provider.setConvertSubErrorCodesToExceptions(true);
        provider.setUseAuthenticationRequestCredentials(true);
        provider.setAuthoritiesMapper(new SimpleAuthorityMapper());
        return provider;
    }

【问题讨论】:

    标签: spring security authentication spring-security spring-boot


    【解决方案1】:

    问题太多了!

    由于您将它们都添加到AuthenticationManagerBuilder,因此这两个提供程序都已启用。但是您将它们都添加到同一个过滤器链中,并且都接受相同类型的 Authentication 作为输入,因此它们中的一个总是屏蔽另一个。

    标准的LdapAuthenticationProvider 有一个authoritiesMapper,您可以使用它来将权限从目录条目映射到您的用户(通常它是使用目录组直接完成的,例如参见sample)。我想如果您的目录不包含组,您可以使所有用户具有相同的权限或使用自定义映射器。

    AuthenticationManagerAuthenticationProvider 类型的 @Beans 看起来很多余(并且可能有害,因为它们是全局的,并且您正在为单个过滤器链配置 AuthenticationManagerBuilder)。我怀疑您根本不需要AuthenticationManager 方法,而另一个不需要公开或@Bean(可能)。

    【讨论】:

    • 这是否意味着我不能同时对两个提供商进行身份验证?在数据库中有超级用户,如果 LDAP 关闭等,可以登录。AuthoritiesMapper 可能会有所帮助,我们可能在 AD 中也有一些组,我会检查。明天试试你的建议。现在谢谢。
    • 如果没有@Bean public AuthenticationManager authenticationManager(),它会完全停止工作:-/
    • 创建了几个 AuthenticationManagerBuilders,我认为应该只有一个。我不明白。
    • 一个应用程序中很容易有多个AuthenticationManagers。它们以不同的方式和不同的代码路径使用。您正在配置的过滤器链是由您提供的WebSecurityConfigurer 创建的本地过滤器链。
    猜你喜欢
    • 2014-08-12
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    相关资源
    最近更新 更多