【问题标题】:Load different authentication provider for certain url为特定 url 加载不同的身份验证提供程序
【发布时间】:2018-10-24 14:19:55
【问题描述】:

我想为不同的 url 加载不同的身份验证提供程序。例如,如果我的 url 匹配“/foo/something”,则加载 FooProvider 和“bar/something”-> BarProvider。问题是当我点击“bar/something”url 时,sessionScope 参数(我在提供者的构造函数中传递它)仍然是“foo”。这意味着,FooProvider 已加载,但这不是我所期望的。 有什么我想念的吗?提前致谢。

 abstract class TokenAuthenticationProvider (
        protected val sessionScope: SessionScope
    ) : AuthenticationProvider { 

    private fun authenticateToken(authentication: TokenAuthentication): Authentication { 
        println("sessionScope $sessionScope")
    }
}

@Component
class FooAuthenticationProvider : TokenAuthenticationProvider (sessionScope = SessionScope.Foo)

@Component
class BarAuthenticationProvider : TokenAuthenticationProvider (sessionScope = SessionScope.Bar)


@Configuration
@EnableWebSecurity
class WebSecurityConfiguration @Autowired constructor(
    private val fooProvider: FooProvider,
    private val barProvider: BarProvider,
    private val authFilter: AuthFilter,
    private val corsFilter: CustomCorsFilter
) : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {

        ... 

        http.authorizeRequests()
            .antMatchers("foo/**")
            .fullyAuthenticated()
            .and()
            .authenticationProvider(fooProvider)

        http.authorizeRequests()
            .antMatchers("bar/**")
            .fullyAuthenticated()
            .and()
            .authenticationProvider(barProvider)

          ... 
    }

    override fun configure(auth: AuthenticationManagerBuilder) {
        auth.authenticationProvider(fooProvider)
        auth.authenticationProvider(barProvider)
    }
}

【问题讨论】:

    标签: spring spring-security kotlin


    【解决方案1】:

    您需要配置两个WebSecurityConfigurerAdapter,并在HttpSecurity的顶层添加一个antMatcher

    @Configuration
    @Order(1)
    class FooWebSecurityConfiguration(val provider: FooProvider) : WebSecurityConfigurerAdapter() {
    
        override fun configure(http: HttpSecurity) {
            http.antMatcher("/foo/**")
                .authorizeRequests()
                .antMatchers("/foo/**")
                .fullyAuthenticated()
                .and()                
                .authenticationProvider(provider)
        }
    }
    
    @Configuration
    @Order(2)
    class BarWebSecurityConfiguration(val provider: BarProvider) : WebSecurityConfigurerAdapter() {
    
        override fun configure(http: HttpSecurity) {
            http.antMatcher("/bar/**")
                .authorizeRequests()
                .antMatchers("/bar/**")
                .fullyAuthenticated()              
                .and()
                .authenticationProvider(provider)
        }
    }
    

    【讨论】:

    • 不幸的是,这个不起作用。 Provider 中的方法 authenticateToken 不执行。我还有一个全局设置配置(例如禁用 csrf 等),带有 Ordered.LOWEST_PRECEDENCE 和 EnableWebSecurity 注释。
    • 每个配置都需要不同的路径,并且只选择一个配置。您可能在订单链的末尾有一个匹配所有设置,但设置不会合并。
    • 我尝试了 2 个相同的配置,但是不同的路径和不同的提供程序,但仍然没有工作。我想,我会通过 AuthFilter 传递 sessionScope。
    猜你喜欢
    • 2019-08-08
    • 2013-02-07
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 2012-04-05
    • 1970-01-01
    相关资源
    最近更新 更多