【问题标题】:Spring Security to support multiple authentication provider LDAP authentication and JPASpring Security 支持多个身份验证提供者 LDAP 身份验证和 JPA
【发布时间】: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


【解决方案1】:

我找到了一个解决方案。我已经在 DB 中设置了启用 LDAP 或 JPA 身份验证的属性,并在运行时根据我调用特定身份验证方法的布尔值加载它们。

【讨论】:

    猜你喜欢
    • 2012-02-18
    • 2016-09-01
    • 2012-02-18
    • 2015-10-20
    • 2011-02-09
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 2016-09-09
    相关资源
    最近更新 更多