gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:53:44 24 4
gpt4 key购买 nike

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

之前:我有我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com