Skip to content

Improve documentation of WebFlux Username/Password login, WebSession persistence. #16926

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

Open
BLCK-B opened this issue Apr 12, 2025 · 0 comments
Labels
status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement

Comments

@BLCK-B
Copy link

BLCK-B commented Apr 12, 2025

Expected Behavior

I have read through the docs and reference and found that more information could be provided about WebFlux Username/Password login. Given the issue described below, I am unsure whether the SecurityContext, WebSession need to be explicitly persisted, if I have to use browser cookies/tokens or if the issue lies with my configuration.

Current Behavior

On login the Security context is saved in a WebSession. On following requests to protected endpoints, the session lookup fails. How is the session associated with the browser?

Example test and output:

webTestClient
  .mutateWith(csrf())
  .post()
  .uri("/auth/login")
  .contentType(MediaType.APPLICATION_JSON)
  .bodyValue(credentials)
  .exchange()
  .expectStatus().isOk();
webTestClient
  .mutateWith(csrf())
  .get()
  .uri("/users/userAccountInfo")
  .exchange()
  .expectStatus().isOk()
WebSessionServerSecurityContextRepository : Saved SecurityContext 'SecurityContextImpl [...], Credentials=[PROTECTED], Authenticated=true, Details=null, Granted Authorities=[USER]]]' in WebSession : 'org.springframework.web.server.session.InMemoryWebSessionStore$InMemoryWebSession@472c317e'

a.DelegatingReactiveAuthorizationManager : Checking authorization on '/users/userAccountInfo' using org.springframework.security.authorization.AuthenticatedReactiveAuthorizationManager@555fdfaf

WebSessionServerSecurityContextRepository : No SecurityContext found in WebSession: 'org.springframework.web.server.session.InMemoryWebSessionStore$InMemoryWebSession@e35c0ee'

Context

WebSessionServerSecurityContextRepository

Username/password authentication and security config:

public Mono<Void> loginUser(ServerWebExchange exchange,
			   Authentication authRequest,
			   ReactiveAuthenticationManager reactiveAuthenticationManager) {
  return reactiveAuthenticationManager.authenticate(authRequest)
  	.flatMap(authResponse -> {
  		SecurityContext securityContext = new SecurityContextImpl(authResponse);
  		return securityContextRepository.save(exchange, securityContext);
  	})
  	.onErrorResume(e -> Mono.error(new InvalidCredentialsException("Invalid credentials: " + e)));
}
@Configuration
@EnableWebFluxSecurity
public class SecurityConfiguration {

	@Bean
	public SecurityWebFilterChain apiFilterChain(ServerHttpSecurity http) {
		http
			.csrf(csrf -> csrf.csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse()))
			.securityContextRepository(new WebSessionServerSecurityContextRepository())
			.authorizeExchange(exchanges -> exchanges
					.pathMatchers("/auth/**").permitAll()
					.anyExchange().authenticated()
			);
		return http.build();
	}

	@Bean
	public WebSessionServerSecurityContextRepository securityContextRepository() {
		return new WebSessionServerSecurityContextRepository();
	}

	@Bean
	public ReactiveUserDetailsService userDetailsService(AccountService accountService) {
		return accountService;
	}

	@Bean
	public ReactiveAuthenticationManager reactiveAuthenticationManager(ReactiveUserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
		UserDetailsRepositoryReactiveAuthenticationManager authenticationManager = new UserDetailsRepositoryReactiveAuthenticationManager(userDetailsService);
		authenticationManager.setPasswordEncoder(passwordEncoder);
		return authenticationManager;
	}

I have already tried asking on StackOverflow.

@BLCK-B BLCK-B added status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement labels Apr 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant