-
Notifications
You must be signed in to change notification settings - Fork 6k
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
SwitchUserFilter not working in Spring Security 6 #12504
Comments
Not sure if it is related but when defining a filter in Spring Boot as an |
Hi @RobertBleyl, thanks for the report. I think that the Can you add the following configuration and see if it solves the problem? If so, that confirms my suspicion. public SecurityFilterChain filterChain(HttpSecurity http) {
http
// ...
.securityContext((securityContext) -> securityContext
.requireExplicitSave(false)
);
return http.build();
} |
Thank you for your reply!
But when trying to perform a user switch an exception is thrown:
I am either doing it wrong or the |
Thank you for your response! I tried it and it unfortunately did not fix the problem. For transparency, this is what the filter chain looked like:
|
@RobertBleyl, I think you should do |
You need to disable it, not enable it! @Bean
public FilterRegistrationBean<SwitchUserFilter> switchUserFilterRegistration(SwitchUserFilter switchUserFilter) {
FilterRegistrationBean<SwitchUserFilter> registration = new FilterRegistrationBean<>(switchUserFilter);
registration.setEnabled(false);
return registration;
} You need to disable it so it won't be added to the regular filter chain, if it would it would execute twice and also far too early in the process. |
Oh wow, my bad! Using |
It does appear that |
Well you want to prevent this filter from being executed twice, so it is always a good idea to add it for filters that are meant only for the security filter chain. |
I just noticed that when trying to access a URL that is not the
I guess this has something to do with I also added the |
I think it is a bug, I'll just confirm with the team first and proceed with the fix. |
I am still experiencing the issue with Spring Boot 3.0.3 (that comes with Spring Security 6.0.2 which appears to include the changes made for this issue). Attached a log file with trace information: The same debug output appears as before: The current version of my SecurityConfig can be found here. I removed the previously added line
Is this "requireExplicitSave" necessary now or was this just for debugging purposes? |
I created a minimal test project demonstrating the issue: https://gitlab.com/robertbleyl/switch-user-test/-/tree/main |
After upgrading spring boot from 2.7 to 3.0.4 I'm experiencing the same issue. After switching the user doesn't get changed. My code is very similar to @RobertBleyl so not sharing it here. Can this issue be reopened? Thanks. |
Hello folks, this was an oversight and I'm working on the fix, I've opened #12834 for that. As a workaround for now, you can configure the Can you try this workaround and see if it works? |
Thank you for your response! I just tried this workaround and it works! |
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
SecurityFilterChain
: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 useAuthorizationFilter
, 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
The text was updated successfully, but these errors were encountered: