spring - 特定 url 的多个身份验证提供程序 - Spring Boot Security

标签 spring spring-boot spring-mvc spring-security spring-security-ldap

在 Spring security 中,我想对以 api/** 开头的 url 使用基本身份验证,对于以/ldap/ 开头的 url 使用 LDAP Rest 身份验证.我的当前代码也允许 ldap/具有基本身份验证。

即使我将它们用作单独的 AuthenticationProviders(如 LdapAuthProvider 和 BasicAuthProvider),问题仍然存在,我如何使用它来指向特定的 url

    @Configuration
    @EnableWebSecurity    
    public class WebSecurityConfig {


        @Configuration
        @Order(1)
        public class BasicAuthenticationProvider extends WebSecurityConfigurerAdapter {


            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.authorizeRequests().antMatchers("/swagger-ui*", "/info", "/health").permitAll()
                .and().authorizeRequests().antMatchers("/order/**").fullyAuthenticated()
                .and().httpBasic().and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and().csrf().disable()
                .anonymous().disable();
            }

            @Override
            protected void configure(AuthenticationManagerBuilder auth) throws Exception {
                auth.userDetailsService(inMemoryUserDetailsManager());
            }
        }

        @Configuration
        @Order(2)
        public class LdapAuthenticationProvider extends WebSecurityConfigurerAdapter {


            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.authorizeRequests().antMatchers("/ldap/**").fullyAuthenticated().and().httpBasic()
                .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().csrf().disable();            
            }

            @Override
            protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// auth.ldapAuthentication() code here......
            }      
        }    
    }

最佳答案

据我了解,您在一个应用程序中有多个入口点,并且有不同类型的用户可以访问应用程序的不同部分。
你应该看看这个 Baeldung 教程:Multiple Entry Points in Spring Security

关于spring - 特定 url 的多个身份验证提供程序 - Spring Boot Security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55213278/

相关文章:

java - 不同队列的多个监听器 - Spring Rabbit

java - 使用 Spring Social、Spring security 登录后重定向到原始 URL?

java.lang.IllegalArgumentException : Cannot instantiate interface org. springframework.context.ApplicationContextInitializer

java - preHandle 中除了 HandlerMethod 之外还有什么处理程序?

spring - 无法让 OpenEntityManagerInViewFilter 在 JBoss 6.1 中工作

java - 如何在 Spring Boot 中国际化 API 错误?

java - Spring 启动验证不起作用

java - 使用 Spring 测试框架模拟服务的预期结果

java - Spring 验证注释 - 如何验证字符串中的 2 个字符条目是否是实际的美国州?

java - 获取 500 内部服务器错误 Spring Boot