Spring 支持多个身份验证提供程序的安全性 LDAP 身份验证和 JPA
Spring Security to support multiple authentication provider LDAP authentication and JPA
我正在尝试为正在进行的 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 的多个文档,但我找不到很清楚的地方
如果有任何可能的解决方案,请告诉我。
提前致谢
我找到了一个解决方案。我已经在数据库中设置了启用 LDAP 或启用 JPA 身份验证的属性,并在 运行 时间加载它们,具体取决于我调用特定身份验证方法的布尔值。
我正在尝试为正在进行的 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 的多个文档,但我找不到很清楚的地方 如果有任何可能的解决方案,请告诉我。 提前致谢
我找到了一个解决方案。我已经在数据库中设置了启用 LDAP 或启用 JPA 身份验证的属性,并在 运行 时间加载它们,具体取决于我调用特定身份验证方法的布尔值。