Sachin
Sachin

Reputation: 527

Session Control with Redis

How do I implement Session Control, when I use @EnableRedisWebSession on my security chain? Spring Session automatically creates beans that implement @websession and @WebsessionStore But the function below, in order to autowire WebSessionStore, needs to see a manual implementation of WebsessionStore, so what do I do?

@Component
internal class SessionControl(
    private val reactiveSessionRegistry: SpringSessionBackedReactiveSessionRegistry<ReactiveRedisIndexedSessionRepository.RedisSession>,
    private val webSessionStore: WebSessionStore
) {

    fun invalidateSessions(username: String): Mono<Void> {
        return reactiveSessionRegistry.getAllSessions(username)
            .flatMap { session ->
                session.invalidate() // invalidate the session
                    .then(webSessionStore.removeSession(session.sessionId)) // remove from WebSessionStore
                    .then(Mono.just(session)) // ensure the session object is returned for logging or further processing if needed
            }
            .then() // complete the Mono after processing all sessions
    }
}

Upvotes: 0

Views: 43

Answers (0)

Related Questions