【问题标题】:Is it possible to have two session management strategies for different apis based on ant matcher基于ant matcher的不同api是否可以有两种会话管理策略
【发布时间】:2020-11-02 07:14:05
【问题描述】:

我正在尝试以下配置代码,以基于 ant matchers 为两个 API 获得不同的会话管理

 http
 .authorizeRequests().antMatchers("/apiV1/**").authenticated()
 .and().sessionManagement()
 .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)

 // How to add in details for custom session cookie for apiV1 ?
 .and().authorizeRequests().antMatchers("/apiV2/**").authenticated()
 .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)

 // How to add in details for custom session cookie for apiV2 ?
 .and().oauth2Login(Customizer.withDefaults());

我还需要在 3 分钟后使 /apiV2 会话无效(可配置)。

是否也可以为这两个 API 维护两个不同的会话 cookie 属性(使用自定义名称),并根据某些业务逻辑使用代码使它们无效?

【问题讨论】:

    标签: java spring-boot spring-security spring-security-oauth2


    【解决方案1】:

    您可以通过使用单独的过滤器链来处理每个请求来做到这一点。 实现 2 个扩展 WebSecurityConfigurerAdapter 的独立配置类

    Have one with: 
    @Configuration
    @Order(1)
    public class SecurityConfig1 extends WebSecurityConfigurerAdapter{
    
        protected void configure(HttpSecurity http) throws Exception {
                http.requestMatcher(""/apiV1/**"")
        .... //
    

    @Configuration
    @Order(2)
    public class SecurityConfig2 extends WebSecurityConfigurerAdapter{
    
        protected void configure(HttpSecurity http) throws Exception {
                http.requestMatcher(""/apiV2/**"")
        ....
    

    Spring会创建2个独立的过滤器链,过滤器链代理会根据请求匹配器将请求路由到每个过滤器链,然后您可以在每个过滤器中自定义Session过滤器等,甚至有不同的身份验证等。

    本文中的更多信息:https://www.baeldung.com/spring-security-multiple-entry-points

    【讨论】:

    • 谢谢,但我已经尝试过了。我的问题是这两个请求都来自同一个浏览器,因此会产生相同的会话 ID。有什么办法解决吗?
    猜你喜欢
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-22
    • 2020-05-18
    • 2014-11-26
    • 1970-01-01
    相关资源
    最近更新 更多