Skip to content

SwitchUserFilter not working in Spring Security 6 #12504

@RobertBleyl

Description

@RobertBleyl

Describe the bug
When using Spring Security 6 (via the Spring Boot 3 BOM) the SwitchUserFilter is not working anymore. The currently logged in user is redirected to the SwitchUserUrl (that is configured in the SwitchUserFilter), but the user is not switched.

The attached log file shows the following line:
"Failed to find original user"

To Reproduce

  • Have a Spring Boot 3 project with Spring Security
  • Define a SwitchUserFilter bean in a configuration class:
	@Bean
	public SwitchUserFilter switchUserFilter() {
		SwitchUserFilter filter = new SwitchUserFilter();
		filter.setUserDetailsService(userDetailsService);
		filter.setUsernameParameter("username");
		filter.setSwitchUserUrl("/admin/switch_user");
		filter.setExitUserUrl("/admin/switch_user_exit");
		filter.setTargetUrl("/");
		return filter;
	}
  • Use this bean in a SecurityFilterChain:
.addFilterAfter(switchUserFilter(), AuthorizationFilter.class)
  • Login as an admin user and try to switch to a different user

Expected behavior
The user performing the switch should be logged in as the selected user.

Sample
While I don't have a minimal example, I have an open source project that reproduces the issue. The relevant config is here:
https://gitlab.com/skrupeltng/skrupel-tng/-/blob/issue-531_spring_boot_3/src/main/java/org/skrupeltng/config/SecurityConfig.java

The javadoc of the SwitchUserFilter still states:
"Note that the filter must come after the FilterSecurityInteceptor in the chain"
However, FilterSecurityIntercepter is deprecated. The deprecation text says one should use AuthorizationFilter, so I used this.
Using the AuthorizationFilter was in fact working when using Spring Boot 2.7 and Spring Security 5.8.
Maybe we have to put the SwitchUserFilter before/after a different Filter now?

switch_user.log

Metadata

Metadata

Labels

in: webAn issue in web modules (web, webmvc)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions