Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Max Session to WebFlux #6192

Closed
bentenzheng opened this issue Nov 30, 2018 · 24 comments
Closed

Add Max Session to WebFlux #6192

bentenzheng opened this issue Nov 30, 2018 · 24 comments
Assignees
Labels
in: config An issue in spring-security-config type: enhancement A general enhancement
Milestone

Comments

@bentenzheng
Copy link

When i am using webflux , how can i achieve a similar effect ,get all login user session ,
like as :
http.sessionManagement() .maximumSessions(1).sessionRegistry(getSessionRegistry()).maxSessionsPreventsLogin(false)

@rwinch rwinch changed the title When i am using webflux , how can i achieve a similar effect ,get all login user session Add Max Session to WebFlux Nov 30, 2018
@rwinch
Copy link
Member

rwinch commented Nov 30, 2018

We do not currently support max sessions in WebFlux. I converted this issue into a ticket for that support

@rwinch rwinch added type: enhancement A general enhancement Reactive labels Nov 30, 2018
@rwinch rwinch added this to the General Backlog milestone Nov 30, 2018
@rwinch rwinch added the status: ideal-for-contribution An issue that we actively are looking for someone to help us with label Nov 30, 2018
@ankurpathak
Copy link
Contributor

Can I give it a try??

@rwinch
Copy link
Member

rwinch commented Dec 3, 2018

@ankurpathak Sure! The ticket is yours

@ankurpathak
Copy link
Contributor

ankurpathak commented Dec 6, 2018

@rwinch I would like to dicusss this ticket progressively:
Here is a top lable flow I am thinking to use:

class AuthenticationWebFilter {
          	private ServerSessionAuthenticationStrategy sessionAuthenticationStrategy = NullAuthenticatedServerSessionStrategy.getInstance();

               private Mono<Void> authenticate(ServerWebExchange exchange,
		WebFilterChain chain, Authentication token) {
		WebFilterExchange webFilterExchange = new WebFilterExchange(exchange, chain);
		return this.authenticationManager.authenticate(token)
			.switchIfEmpty(Mono.defer(() -> Mono.error(new IllegalStateException("No provider found for " + token.getClass()))))
			.flatMap(authentication -> sessionAuthenticationStrategy.onAuthentication(authentication, webFilterExchange.getExchange()).then(Mono.just(authentication)))
			.flatMap(authentication -> onAuthenticationSuccess(authentication, webFilterExchange))
			.onErrorResume(AuthenticationException.class, e -> this.authenticationFailureHandler
				.onAuthenticationFailure(webFilterExchange, e));
	}
}

public interface ServerSessionAuthenticationStrategy {
	Mono<Void> onAuthentication(Authentication authentication, ServerWebExchange exchange);
}

public class  NoOpServerSessionAuthenticationStrategy implements ServerSessionAuthenticationStrategy {
	Mono<Void> onAuthentication(Authentication authentication, ServerWebExchange exchange){
             return Mono.empty();
        }

}

public interface ConcurrentServerSessionAuthenticationStrategy implements ServerSessionAuthenticationStrategy{
	Mono<Void> onAuthentication(Authentication authentication, ServerWebExchange exchange){
             return Mono.error(new SessionAuthenticationException("Error"));
        }

}
  1. AuthenticationWebFilter will have instanace of ServerSessionAuthenticationStrategy
  2. After authentication check session counts delegating it to ServerSessionAuthenticationStrategy
  3. Two implementation of ServerSessionAuthenticationStrategy NoOp and Concurrent
    4 Support for adding ServerSessionAuthenticationStrategy in all AuthenticationWebFilter in ServerHttpSecurity

@ankurpathak
Copy link
Contributor

ankurpathak commented Dec 13, 2018

@rwinch What is equivalent of SessionRegistry in reactive world?
I think we would need ReactiveSessionRegistry for this issue as using SessionRegistry may be non
blocking for InMemoryImplementatio but may be blocking for SessionRegistry implemenation for
Redis, Mongo, JDBC in Spring Session. What do you think?

@rwinch
Copy link
Member

rwinch commented Dec 18, 2018

We don't currently have an equivalent. Note: I'd try and reimagine the API a bit rather than copy it directly. See exactly what is needed by Spring Security and limit it to that.

@rwinch rwinch removed the Reactive label May 6, 2019
@issilin
Copy link

issilin commented Sep 12, 2019

@rwinch
Is this ticket alive? Can I try to solve this?

@mukeshkamboj
Copy link

@rwinch
Do we have any solution?

@ankurpathak
Copy link
Contributor

@mukeshkamboj @rwinch I am not working on this.

@issilin
Copy link

issilin commented Mar 14, 2020

@rwinch
Still relevant? May I suggest a solution?

@Johannes-Rost
Copy link

Is this ticket still considered relevant?
The ability to control the number of parallel sessions is often requested in reviews as a security function.

@rishisc
Copy link

rishisc commented Jul 2, 2021

Is there any workaround for it in webflux using ReactiveRedisSessionRepository

@Danushka96
Copy link

Any progress of this issue?

@SuveenVundavalli
Copy link

Hi, any progress or suggestions on this?

@yangdq1
Copy link

yangdq1 commented Nov 22, 2021

Hi, any progress or suggestions on this issue ?

@maradanasai
Copy link

Hi, any progress or suggestions on this?

1 similar comment
@RajeevKumarMarrapu
Copy link

Hi, any progress or suggestions on this?

@dgallego58
Copy link

this seems to be a long way, since authorization server is focused in Oauth2 patterns with spring security team i don't see coming this feature soon

@JonnyDeates
Copy link

Any Progress on this?

@marcusdacoregio
Copy link
Contributor

Hi everyone, that has been no progress yet on this. Please give a thumbs up to the root comment if you would like to see this feature in Spring Security, this helps us prioritize issues.

If anyone is interested in working on this feature, you are welcome.

@marcusdacoregio marcusdacoregio self-assigned this May 31, 2023
@marcusdacoregio marcusdacoregio moved this from Planning to Prioritized in Spring Security Team Jul 3, 2023
@marcusdacoregio marcusdacoregio added in: config An issue in spring-security-config and removed status: ideal-for-contribution An issue that we actively are looking for someone to help us with labels Jul 5, 2023
@marcusdacoregio marcusdacoregio removed this from the General Backlog milestone Jul 10, 2023
@marcusdacoregio marcusdacoregio added this to the 6.3.0-M1 milestone Nov 24, 2023
marcusdacoregio added a commit to marcusdacoregio/spring-security that referenced this issue Dec 11, 2023
@marcusdacoregio
Copy link
Contributor

marcusdacoregio commented Feb 6, 2024

For those waiting for this, the initial support is available in Spring Security 6.3.0-M1, please give it a try and, if possible, report any issues/enhancements. The docs are available at https://docs.spring.io/spring-security/reference/6.3/reactive/authentication/concurrent-sessions-control.html

marcusdacoregio added a commit that referenced this issue Feb 6, 2024
The reactive implementation of max sessions does not keep track of expired sessions, therefore we do not need such parameter

Issue gh-6192
marcusdacoregio added a commit that referenced this issue Feb 27, 2024
This commit changes the PreventLoginServerMaximumSessionsExceededHandler to invalidate the WebSession in addition to throwing the error, this is needed otherwise the session would still be saved with the security context. It also changes the SessionRegistryWebSession to first perform the operation on the delegate and then invoke the needed method on the ReactiveSessionRegistry

Issue gh-6192
marcusdacoregio added a commit that referenced this issue Feb 27, 2024
Add WebSessionStore constructor parameter to the handler

Issue gh-6192
marcusdacoregio added a commit that referenced this issue Feb 28, 2024
Delete WebSessionStoreReactiveSessionRegistry.java and gives the responsibility to remove the sessions from the WebSessionStore to the handler

Issue gh-6192
@jsantana3c
Copy link

the example seems to not be working on the 3.3.0-RC1 (I'm using OAuthLogin),

this is my config:

 @Bean
    SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
        http
                .authorizeExchange(authorizeRequests -> authorizeRequests
                        .anyExchange()
                        .authenticated()
                )
                .sessionManagement(sessions -> sessions
                        .concurrentSessions(concurrency -> concurrency
                                .maximumSessions(SessionLimit.of(1))
                                .maximumSessionsExceededHandler(new PreventLoginServerMaximumSessionsExceededHandler())
                        )
                )
                .csrf(ServerHttpSecurity.CsrfSpec::disable)
                .oauth2Login(withDefaults())
                .logout(logout -> logout
                        .logoutSuccessHandler(oidcLogoutSuccessHandler())
                )
                .oauth2Client(withDefaults());

        return http.build();
    }

@Bean
    ReactiveSessionRegistry reactiveSessionRegistry() {
        return new InMemoryReactiveSessionRegistry();
    }

@marcusdacoregio
Copy link
Contributor

Hi @jsantana3c, can you provide a minimal, reproducible sample?

@jsantana3c
Copy link

Here you go,
I created both authorization server and gateway

https://github.com/joaquinjsb/security-oauth-example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: config An issue in spring-security-config type: enhancement A general enhancement
Projects
Archived in project
Development

No branches or pull requests