当前位置: 首页 > 知识库问答 >
问题:

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

施俊明
2023-03-14

我正在尝试为正在进行的 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)获取我的应用程序的身份验证方法,并执行相应的身份验证提供者方法?

我已经查看了MultipleAuth的多个文档,但我找不到太多清晰度,请告诉我是否有任何可能的解决方案。提前感谢

共有1个答案

邹桐
2023-03-14

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

 类似资料:
  • 我的问题是,我希望有两个身份验证提供商 之前:我有我的UserDetailServiceImpl,我根据数据库中的数据验证了用户的身份(不确定是哪个提供者) 现在:我使用了ActiveDirectoryLdapAuthentiation提供程序,如下所示 我成功了,所以我可以认证。 问题是: 我现在无法再使用数据库用户登录,现在只有LDAP。 未使用UserDetailsService,因此用户具

  • 在 spring security 中有几个对多个身份验证提供程序的引用,但在 Java config 中找不到示例。 以下链接给出了XML符号:Spring Security中的多个身份验证提供者 我们需要使用LDAP或DB进行身份验证 下面是我们的示例代码:

  • 现在,我们有了集成执行器。我希望我的执行器endpoint可以通过基本认证在浏览器中访问。 为此,我用@order(1)添加了WebSecurityConfigurerAdapter的一个实现。它在浏览器上工作得很好。但是,当我从angualar应用程序调用登录url时,它为/oauth/token url给出了401个未经授权的错误,因此我无法从ui应用程序登录。 任何帮助将感谢解决此错误。 类

  • 问题内容: 我的Web应用程序有多个身份验证管理器(一个用于API,一个用于WEB访问)。该api应该仅具有基本的身份验证服务- 通过spring安全标记进行配置,如下所示: 我无法内联身份验证提供程序,因为我希望它可以被子bean配置覆盖。 我的问题是我无法在security:authentication-provider元素上定义别名/ id以在身份验证管理器中引用它。有一个简单的解决方法吗?

  • 我想为Spring Security配置L 我配置spring-security.xml指出m 如何设置为 Spring,使用我的自定义类而不是他的 deafaul LDAP 身份验证提供程序?