【问题标题】:Multiple authentication provider for specific url - Spring Boot Security特定 url 的多个身份验证提供程序 - Spring Boot Security
【发布时间】:2019-08-08 08:15:26
【问题描述】:

在 Spring 安全性中,我想对以 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......
            }      
        }    
    }

【问题讨论】:

  • 我已经添加了我尝试过的代码,它还允许 /ldap/** 具有让我困惑的基本身份验证
  • 你有没有想过这个问题?

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


【解决方案1】:

据我了解,您在一个应用程序中有多个入口点,并且有不同类型的用户可以访问应用程序的不同部分。

你应该看看这个 Baeldung 教程:Multiple Entry Points in Spring Security

【讨论】:

    猜你喜欢
    • 2012-02-18
    • 2012-03-07
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 2016-05-23
    • 2015-04-10
    • 2011-02-09
    • 1970-01-01
    相关资源
    最近更新 更多