併發會話控制

Servlet 的併發會話控制類似,Spring Security 也支援在 Reactive 應用程式中限制使用者可以擁有的併發會話數量。

當您在 Spring Security 中設定併發會話控制時,它透過監聽表單登入、OAuth 2.0 登入和 HTTP Basic 認證的認證成功處理方式來監控這些認證。更具體地說,會話管理 DSL 會將 ConcurrentSessionControlServerAuthenticationSuccessHandlerRegisterSessionServerAuthenticationSuccessHandler 新增到認證過濾器使用的 ServerAuthenticationSuccessHandler 列表中。

以下章節包含如何配置併發會話控制的示例。

限制併發會話

預設情況下,Spring Security 允許使用者擁有任意數量的併發會話。要限制併發會話的數量,您可以使用 maximumSessions DSL 方法

為任何使用者配置一個會話
  • Java

  • Kotlin

@Bean
SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
    http
        // ...
        .sessionManagement((sessions) -> sessions
            .concurrentSessions((concurrency) -> concurrency
                .maximumSessions(SessionLimit.of(1))
            )
        );
    return http.build();
}

@Bean
ReactiveSessionRegistry reactiveSessionRegistry() {
    return new InMemoryReactiveSessionRegistry();
}
@Bean
open fun springSecurity(http: ServerHttpSecurity): SecurityWebFilterChain {
    return http {
        // ...
        sessionManagement {
            sessionConcurrency {
                maximumSessions = SessionLimit.of(1)
            }
        }
    }
}
@Bean
open fun reactiveSessionRegistry(): ReactiveSessionRegistry {
    return InMemoryReactiveSessionRegistry()
}

上述配置允許任何使用者擁有一個會話。同樣,您也可以使用 SessionLimit#UNLIMITED 常量來允許無限會話

配置無限會話
  • Java

  • Kotlin

@Bean
SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
    http
        // ...
        .sessionManagement((sessions) -> sessions
            .concurrentSessions((concurrency) -> concurrency
                .maximumSessions(SessionLimit.UNLIMITED))
        );
    return http.build();
}

@Bean
ReactiveSessionRegistry reactiveSessionRegistry() {
    return new InMemoryReactiveSessionRegistry();
}
@Bean
open fun springSecurity(http: ServerHttpSecurity): SecurityWebFilterChain {
    return http {
        // ...
        sessionManagement {
            sessionConcurrency {
                maximumSessions = SessionLimit.UNLIMITED
            }
        }
    }
}
@Bean
open fun reactiveSessionRegistry(webSessionManager: WebSessionManager): ReactiveSessionRegistry {
    return InMemoryReactiveSessionRegistry()
}

由於 maximumSessions 方法接受一個 SessionLimit 介面,而該介面又擴充套件了 Function<Authentication, Mono<Integer>>,因此您可以擁有更復雜的邏輯來根據使用者的認證確定最大會話數量

根據 Authentication 配置 maximumSessions
  • Java

  • Kotlin

@Bean
SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
    http
        // ...
        .sessionManagement((sessions) -> sessions
            .concurrentSessions((concurrency) -> concurrency
                .maximumSessions(maxSessions()))
        );
    return http.build();
}

private SessionLimit maxSessions() {
    return (authentication) -> {
        if (authentication.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_UNLIMITED_SESSIONS"))) {
            return Mono.empty(); // allow unlimited sessions for users with ROLE_UNLIMITED_SESSIONS
        }
        if (authentication.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_ADMIN"))) {
            return Mono.just(2); // allow two sessions for admins
        }
        return Mono.just(1); // allow one session for every other user
    };
}

@Bean
ReactiveSessionRegistry reactiveSessionRegistry() {
    return new InMemoryReactiveSessionRegistry();
}
@Bean
open fun springSecurity(http: ServerHttpSecurity): SecurityWebFilterChain {
    return http {
        // ...
        sessionManagement {
            sessionConcurrency {
                maximumSessions = maxSessions()
            }
        }
    }
}

fun maxSessions(): SessionLimit {
    return { authentication ->
        if (authentication.authorities.contains(SimpleGrantedAuthority("ROLE_UNLIMITED_SESSIONS"))) Mono.empty
        if (authentication.authorities.contains(SimpleGrantedAuthority("ROLE_ADMIN"))) Mono.just(2)
        Mono.just(1)
    }
}

@Bean
open fun reactiveSessionRegistry(): ReactiveSessionRegistry {
    return InMemoryReactiveSessionRegistry()
}

當會話數量超過最大值時,預設情況下,最近最少使用的會話將被過期。如果您想更改此行為,可以自定義當會話數量超過最大值時使用的策略

併發會話管理並不知道您可能透過 OAuth 2 登入等方式使用的某些身份提供商中是否存在其他會話。如果您還需要使身份提供商的會話失效,則必須包含您自己的 ServerMaximumSessionsExceededHandler 實現

處理會話數量超出最大值

預設情況下,當會話數量超過最大值時,透過使用 InvalidateLeastUsedServerMaximumSessionsExceededHandler,最近最少使用的會話將被過期。Spring Security 還提供了另一種實現,透過使用 PreventLoginServerMaximumSessionsExceededHandler 來阻止使用者建立新會話。如果您想使用自己的策略,可以提供不同的 ServerMaximumSessionsExceededHandler 實現。

配置 maximumSessionsExceededHandler
  • Java

  • Kotlin

@Bean
SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
    http
        // ...
        .sessionManagement((sessions) -> sessions
            .concurrentSessions((concurrency) -> concurrency
                .maximumSessions(SessionLimit.of(1))
                .maximumSessionsExceededHandler(new PreventLoginMaximumSessionsExceededHandler())
            )
        );
    return http.build();
}

@Bean
ReactiveSessionRegistry reactiveSessionRegistry() {
    return new InMemoryReactiveSessionRegistry();
}
@Bean
open fun springSecurity(http: ServerHttpSecurity): SecurityWebFilterChain {
    return http {
        // ...
        sessionManagement {
            sessionConcurrency {
                maximumSessions = SessionLimit.of(1)
                maximumSessionsExceededHandler = PreventLoginMaximumSessionsExceededHandler()
            }
        }
    }
}

@Bean
open fun reactiveSessionRegistry(): ReactiveSessionRegistry {
    return InMemoryReactiveSessionRegistry()
}

指定 ReactiveSessionRegistry

為了跟蹤使用者的會話,Spring Security 使用 ReactiveSessionRegistry,並且每次使用者登入時,他們的會話資訊都會被儲存。

Spring Security 提供了 ReactiveSessionRegistryInMemoryReactiveSessionRegistry 實現。

要指定 ReactiveSessionRegistry 實現,您可以將其宣告為一個 bean

作為 Bean 的 ReactiveSessionRegistry
  • Java

  • Kotlin

@Bean
SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
    http
        // ...
        .sessionManagement((sessions) -> sessions
            .concurrentSessions((concurrency) -> concurrency
                .maximumSessions(SessionLimit.of(1))
            )
        );
    return http.build();
}

@Bean
ReactiveSessionRegistry reactiveSessionRegistry() {
    return new MyReactiveSessionRegistry();
}
@Bean
open fun springSecurity(http: ServerHttpSecurity): SecurityWebFilterChain {
    return http {
        // ...
        sessionManagement {
            sessionConcurrency {
                maximumSessions = SessionLimit.of(1)
            }
        }
    }
}

@Bean
open fun reactiveSessionRegistry(): ReactiveSessionRegistry {
    return MyReactiveSessionRegistry()
}

或者您可以使用 sessionRegistry DSL 方法

使用 sessionRegistry DSL 方法的 ReactiveSessionRegistry
  • Java

  • Kotlin

@Bean
SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
    http
        // ...
        .sessionManagement((sessions) -> sessions
            .concurrentSessions((concurrency) -> concurrency
                .maximumSessions(SessionLimit.of(1))
                .sessionRegistry(new MyReactiveSessionRegistry())
            )
        );
    return http.build();
}
@Bean
open fun springSecurity(http: ServerHttpSecurity): SecurityWebFilterChain {
    return http {
        // ...
        sessionManagement {
            sessionConcurrency {
                maximumSessions = SessionLimit.of(1)
                sessionRegistry = MyReactiveSessionRegistry()
            }
        }
    }
}

手動使註冊使用者的會話失效

有時,能夠使使用者的所有或部分會話失效非常方便。例如,當用戶更改密碼時,您可能希望使其所有會話失效,以強制他們重新登入。要做到這一點,您可以使用 ReactiveSessionRegistry bean 來檢索使用者的所有會話,使其失效,然後從 WebSessionStore 中刪除它們

使用 ReactiveSessionRegistry 手動使會話失效
  • Java

public class SessionControl {
    private final ReactiveSessionRegistry reactiveSessionRegistry;

    private final WebSessionStore webSessionStore;

    public Mono<Void> invalidateSessions(String username) {
        return this.reactiveSessionRegistry.getAllSessions(username)
            .flatMap((session) -> session.invalidate().thenReturn(session))
            .flatMap((session) -> this.webSessionStore.removeSession(session.getSessionId()))
            .then();
    }
}

為某些認證過濾器停用它

預設情況下,只要表單登入、OAuth 2.0 登入和 HTTP Basic 認證本身沒有指定 ServerAuthenticationSuccessHandler,併發會話控制就會自動為其配置。例如,以下配置將停用表單登入的併發會話控制

停用表單登入的併發會話控制
  • Java

  • Kotlin

@Bean
SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
    http
        // ...
        .formLogin((login) -> login
            .authenticationSuccessHandler(new RedirectServerAuthenticationSuccessHandler("/"))
        )
        .sessionManagement((sessions) -> sessions
            .concurrentSessions((concurrency) -> concurrency
                .maximumSessions(SessionLimit.of(1))
            )
        );
    return http.build();
}
@Bean
open fun springSecurity(http: ServerHttpSecurity): SecurityWebFilterChain {
    return http {
        // ...
        formLogin {
            authenticationSuccessHandler = RedirectServerAuthenticationSuccessHandler("/")
        }
        sessionManagement {
            sessionConcurrency {
                maximumSessions = SessionLimit.of(1)
            }
        }
    }
}

新增額外的成功處理程式而不停用併發會話控制

您也可以在不停用併發會話控制的情況下,將額外的 ServerAuthenticationSuccessHandler 例項新增到認證過濾器使用的處理程式列表中。為此,您可以使用 authenticationSuccessHandler(Consumer<List<ServerAuthenticationSuccessHandler>>) 方法

新增額外的處理程式
  • Java

@Bean
SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
    http
        // ...
        .formLogin((login) -> login
            .authenticationSuccessHandler((handlers) -> handlers.add(new MyAuthenticationSuccessHandler()))
        )
        .sessionManagement((sessions) -> sessions
            .concurrentSessions((concurrency) -> concurrency
                .maximumSessions(SessionLimit.of(1))
            )
        );
    return http.build();
}

檢視示例應用程式

© . This site is unofficial and not affiliated with VMware.