-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Resolve Principal argument only when not annotated #25780
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fcca5f5
Disable ArgumentResolver for annotated Principal
anthonyraymond dec530a
Add missing import
anthonyraymond b014ff5
Add test to ensure non-regression
anthonyraymond 2dfebc0
Restore imports order according to project guidelines for imports
anthonyraymond File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merely checking for the absence of an annotation would not be sufficient, since the parameter could be annotated with annotations other than Spring Security's
@AuthenticationPrincipal
.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the Spring Security issue would need to be addressed within Spring Security, potentially making use of
RequestMappingHandlerAdapter.setArgumentResolvers(List<HandlerMethodArgumentResolver>)
(as opposed tosetCustomArgumentResolvers(...)
).@rstoyanchev and @rwinch, thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think setting the default resolvers is a good way to go.
We have some precedent with arguments of type
Map
where we do check that it isn't annotated in some way, see 5a3ff35. We could so something similar here given thatPrincipal
is also a very high level interface with many possible uses and implementations. What I'm wondering though is why this problem surfaces only now, was there some change in Spring Security or did it never work?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or does it depend on the user Object of the application?
Under spring-projects/spring-security#4151 it says one option is for UserDetails to not inherit Principal but from what I see UserDetails doesn't .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I wasn't aware of that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it depends on the user-specific implementation of
UserDetails
.Right. The
UserDetails
interface does not extendPrincipal
by default, though a developer may of course choose to implementPrincipal
if they want (or need) to.I have implemented in
UserDetails
in several applications of the years, and I have never had it implementPrincipal
as well. I also have never seen anyone do that. But... as I mentioned above, it's certainly an option if it makes sense in a given application.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi guys thanks for your investment in this issue.
to answer @rstoyanchev
My guess is that it's never worked, people use the
@AuthenticationPrincipal
and they get what they asks for: aPrincipal
. It's not the expected one, because it's not coming from the right place, but it's a principal and it's good enough after 3 hours of tweaking the Auth process to get what you want🤝.AllMost peoples HATES having to deal with auth 😨, and if the framework is handling this good enough that's a deal 😄 .But this behavior has always been a problem:
HttpServletRequest.getUserPrincipal()
contains theAuthentication
(which extends Principal) so it returns it, and it does not extract the Principal from the Authentication. There are multiple solutions for this around SO and most of them say : Useorg.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken
in place ofKeycloakPrincipal
(which is an implementation ofAuthentication
) and magically it starts to work, but not for the good reasons.HttpServletRequest.getUserPrincipal()
contains theAuthentication
(which extends Principal).Basically what you can get with the Google search "Current user principal is not of type" is related to this issue.
Both of these issues come from the fact that the Principal resolved is from
HttpServletRequest.getUserPrincipal()
instead ofSecurityContext.getAuthentication.getPrincipal()
.The nasty thing about that is that the
@AuthenticationPrincipal
has not only become not relevant but also missleading, because all class extending Principal can be injected into aPrincipal
type (thanks to the spring-framework, the annotation takes no part in this), but as soon as you want to get your implementation ofPrincipal
you are doomed.A side question that i can't figure out on my own (no sarcasm, that's a real question):
spring-framework
trying to inject a Principal ? Isn'tjava.security.Principal
informaly but well accepted as an authentication class? IMHO it makes more sense to delegate that behaviour to thesecurity
part of spring.