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

SecurityContextHolderStrategy bean should be copied to SecurityContextHolder by default #11886

Open
Tracked by #10973
jzheaux opened this issue Sep 20, 2022 · 2 comments
Labels
in: config An issue in spring-security-config type: enhancement A general enhancement

Comments

@jzheaux
Copy link
Contributor

jzheaux commented Sep 20, 2022

Now that applications can use a SecurityContextHolderStrategy bean, there's value in assisting applications with what may be a sizeable transition away from using SecurityContextHolder.

There are some cases where an application may not want to copy the SecurityContextHolderStrategy bean into SecurityContextHolder's static field:

  • The application may not want to risk potential memory leaks from setting a bean to a static field
  • The application uses more than one application context, each with a SecurityContextHolderStrategy. Given that arrangement, it is undefined which bean will be set to the static field.
  • The application may want to discourage the use of static lookups.
  • The application may otherwise need more control over deciding what goes into the static field.

Given the foundational nature of SecurityContextHolder in Spring Security, it's likely that applications for a time will want to assume the above risks. As such, this should be property-driven.

One way to do this is to introduce useSecurityContextHolder into @EnableWebSecurity. It would default to true.

If useSecurityContextHolder is true, then Spring Security will look for a SecurityContextHolderStrategy bean and call SecurityContextHolder.setSecurityContextHolderStrategy. If it's false, then no action will be taken.

@jzheaux jzheaux changed the title SecurityContextHolderStrategy bean should be copied to SecurityContextHolder by default SecurityContextHolderStrategy bean should be copied to SecurityContextHolder by default Sep 20, 2022
@jzheaux jzheaux added this to the General Backlog milestone Sep 20, 2022
@jzheaux jzheaux added in: config An issue in spring-security-config type: enhancement A general enhancement labels Sep 20, 2022
@kumper
Copy link

kumper commented Nov 23, 2023

Hello, any chance to have this implemented any time soon? It's open for over a year now and in my project we were also suffering from this. Finally we applied a workaround suggested in another issue, but it would be great to have it sorted out-of-the-box. BTW: we also have a shared security library, which sets reasonable security defaults, but then SecurityContextHolderStrategy is not shared between the components (filters in our case).

@landsman
Copy link

Yeah, this is definitely pain.

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
Status: No status
Development

No branches or pull requests

3 participants