【发布时间】:2020-11-09 07:08:22
【问题描述】:
我正在尝试为正在进行的 spring-boot 项目实施身份验证和授权服务。我已经实现了一个基于 JPA 的身份验证提供程序,它工作正常。如何将 LDAP 身份验证提供程序添加到同一个项目并根据用户身份验证类型在身份验证方法之间切换?
下面是我的代码
@Configuration
public class ProjectConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UsernamePasswordAuthProvider authProvider;
@Autowired
private LdapAuth ldapAuth;
@Autowired
private LdapAuthenticationpopulator ldapAuthenticationpopulator;
private String ldapUrls = "ldap://localhost:3890";
private String ldapSecurityPrincipal = "cn=admin,dc=mycompany,dc=com";
private String ldapPrincipalPassword = "admin";
private String userDnPattern = "uid={0}";
@Autowired
private UsernamePasswordAuthFilter usernamePasswordAuthFilter;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Order(1)
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication()
.contextSource()
.url(ldapUrls)
.managerDn(ldapSecurityPrincipal)
.managerPassword(ldapPrincipalPassword)
.and()
.userDnPatterns(userDnPattern)
.ldapAuthoritiesPopulator(ldapAuthenticationpopulator);
}
@Override
@Order(2)
protected void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(authProvider);
}
@Override
protected void configure(HttpSecurity http) {
http.addFilterAt(usernamePasswordAuthFilter,
BasicAuthenticationFilter.class);
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
虽然我的 LDAP 凭据是正确的,但它没有达到该方法。 如何从 DB ex(LDAP, JPA, SSO) 为我的应用程序获取身份验证方法并执行相应的身份验证提供程序方法?
我浏览了 MultipleAuthenticationProviders 的多个文档,但我找不到很清楚的地方 请让我知道是否有任何可能的解决方案。 提前致谢
【问题讨论】:
-
我想你可以去掉 configureGlobal 并在 configure 方法中添加 ldap 信息:
auth.authenticationProvider(authProvider).ldapAuthentication()....; -
之前也尝试过,它首先会转到
auth.authenticationProvider(authProvider),它会给出错误凭据的错误。然后请求将因异常而退出执行。 -
如果用户和密码不存在,我认为您需要从第一个身份验证提供者返回 null,以便链中的下一个身份验证提供者可以进行身份验证。
标签: java spring spring-boot spring-security ldap