简体   繁体   English

Spring 安全性支持多个身份验证提供程序 LDAP 身份验证和 JPA

[英]Spring Security to support multiple authentication provider LDAP authentication and JPA

I'm trying to implement an authentication and authorization service for an ongoing spring-boot project.我正在尝试为正在进行的 spring-boot 项目实施身份验证和授权服务。 I have implemented a JPA based authentication provider and it is working fine.我已经实现了一个基于 JPA 的身份验证提供程序,它工作正常。 How do I add LDAP authentication provider to the same project and switch between the authentication methods depending on the user authentication type?如何将 LDAP 身份验证提供程序添加到同一项目并根据用户身份验证类型在身份验证方法之间切换?

Below is my code下面是我的代码

@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();
    }
}

Though my LDAP credentials are correct it is not reaching that method.尽管我的 LDAP 凭据是正确的,但它没有达到该方法。 How do I get the authentication method from DB ex(LDAP, JPA, SSO) for my application and execute the corresponding auth provider method?如何从 DB ex(LDAP, JPA, SSO) 为我的应用程序获取身份验证方法并执行相应的身份验证提供程序方法?

I have gone through multiple documents for MultipleAuthenticationProviders but I couldn't find much clarity Please let me know if there is any possible solution for this.我已经浏览了 MultipleAuthenticationProviders 的多个文档,但我找不到很清楚的地方,如果有任何可能的解决方案,请告诉我。 Thanks in advance提前致谢

I found one solution to this.我找到了一个解决方案。 I have put up LDAP enabled or JPA auth enabled properties in DB and loading them at run time depending upon the boolean value I'm calling the particular auth method.我已经在 DB 中启用了 LDAP 或 JPA 启用了身份验证的属性,并在运行时根据 boolean 值加载它们我正在调用特定的身份验证方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM