Spring Boot 安全性 - 多个身份验证提供程序

标签 spring security authentication spring-security spring-boot

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

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

现在: 我使用了 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;
    }

最佳答案

问题太多了!

自从您将两个提供程序都添加到 AuthenticationManagerBuilder 后,这两个提供程序均已启用。但是您将它们都添加到同一个过滤器链中,并且都接受相同类型的身份验证作为输入,因此其中一个始终会掩盖另一个。

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

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

关于Spring Boot 安全性 - 多个身份验证提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28434852/

相关文章:

java - 在 Spring Boot 上动态设置 Spring AMQP 和 RabbitMQ 的主机

java - Spring 自动扫描组件失败

java - 无法捕获和处理java中的spring自定义事件

spring - java.lang.NoClassDefFoundError : org/slf4j/LoggerFactory Spring boot 1. 5.2 Maven

Java Web 应用程序部署

javascript - 使用 AJAX 从 MongoDB 获取用户信息

php - 这段 PHP 代码有什么错误?

php - 用于密码存储的 SHA1 哈希有什么好的替代方案?

c# - 使用Windows帐户SID作为安全 token

asp.net-mvc - 登录重定向后如何传递用户名?