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

@PreAuthorize: authentication is resolved to null from the security context holder #13641

Closed
libetl opened this issue Aug 9, 2023 · 5 comments
Assignees
Labels
in: core An issue in spring-security-core status: duplicate A duplicate of another issue type: bug A general bug

Comments

@libetl
Copy link

libetl commented Aug 9, 2023

Describe the bug

Hello,
Unfortunately @PreAuthorize does not work for me after upgrading to spring 6.0.11 with spring-security 6.1.2. (it used to work fine with spring 5.3.8)

private Supplier<Authentication> authentication = getAuthentication(
SecurityContextHolder.getContextHolderStrategy());

I don't understand why the security context holder is resolved only once at startup time
(created from : static MethodInterceptor preAuthorizeAuthorizationMethodInterceptor)

Because of that, the SecurityContextHolder used to resolved the authentication is always the one resolved from the Bean Initialization process... So it is always empty.

To Reproduce
Steps to reproduce the behavior.
Use a @PreAuthorize annotation on a method in a spring boot application (spring boot 3)

Expected behavior
A clear and concise description of what you expected to happen.
The authentication should be resolved at runtime. And if the authentication exists, it should be resolved.

What I would do

I would remove the parameter from AuthorizationManagerBeforeMethodInterceptor.getAuthentication and instead call SecurityContextHolder.getContextHolderStrategy() in the Supplier lambda.

@libetl libetl added status: waiting-for-triage An issue we've not yet triaged type: bug A general bug labels Aug 9, 2023
@libetl
Copy link
Author

libetl commented Aug 9, 2023

this is probably linked to #12877

@rishiraj88
Copy link
Contributor

Waiting...

@rishiraj88
Copy link
Contributor

Thanks, @libetl

@libetl
Copy link
Author

libetl commented Aug 10, 2023

I think I'll just override the Security Context Holder mode to be only Thread Local.

@jzheaux jzheaux added in: core An issue in spring-security-core and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 7, 2023
@jzheaux jzheaux self-assigned this Nov 7, 2023
@jzheaux jzheaux added the status: duplicate A duplicate of another issue label Nov 7, 2023
@jzheaux
Copy link
Contributor

jzheaux commented Nov 7, 2023

This is fixed by #12877

@jzheaux jzheaux closed this as completed Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core An issue in spring-security-core status: duplicate A duplicate of another issue type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants