Skip to content

AuthenticationPrincipalArgumentResolver not used when argument class implements Principal #4151

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
ghost opened this issue Dec 8, 2016 · 5 comments
Assignees
Labels
for: external-project For an external project and not something we can fix type: enhancement A general enhancement

Comments

@ghost
Copy link

ghost commented Dec 8, 2016

Summary

putting an @AuthenticationPrincipal annotation on a Controller parameter, where the parameter's type implements Principal causes the servlet to throw an IllegalArgumentException

Actual Behavior

Basically, since ServletRequestMethodArgumentResolver supports Principal objects, it gets used, and it populates the parameter with an Authentication object, causeing a casting exception when the resolving AuthenticationObject is forced into my UserDetails object

Expected Behavior

AuthenticationPrincipalArgumentResolver should be defaulted to resolve AuthenticationPrincipal arguments, which will do the correct resolution.

The workaround is not having my UserDetails object inherit Principal, or use the actual UserDetails class in the parameter, and then cast it to my subclass, but this is silly. It makes perfect sense that a UserDetails object is also a Princial, and it is in fact retrieved via Authentication Principal

Fix

The fix would be forcing ServletRequestMethodArgumentResolver to be lower priority in argument resolution than AuthenticationPrincipalArgumentResolver

Version

All

@ghost ghost changed the title AuthenticationPrincipalArgumentResolver not used when UserDetails object implements Principal AuthenticationPrincipalArgumentResolver not used when argument class implements Principal Dec 9, 2016
@reliveyy
Copy link

Found the same issue.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 7, 2019
@anthonyraymond
Copy link

anthonyraymond commented Sep 16, 2020

Still an issue in spring-security:5.3.4

@sbrannen
Copy link
Member

See comments in spring-projects/spring-framework#25780 (review)

@eleftherias eleftherias self-assigned this Mar 24, 2022
@eleftherias
Copy link
Contributor

Note that to reproduce this issue users must implement Principal and inject their custom class using the @AuthenticationPrincipal annotation.

 class CustomUserDetails implements UserDetails, Principal {
    // ....
@GetMapping("/test")
public String test(@AuthenticationPrincipal CustomUserDetails user) {
    // ...

@eleftherias
Copy link
Contributor

I can confirm that the associated issue spring-projects/spring-framework#25780 fixes this problem.

Since this was fixed in Spring Framework 5.3, it is fixed in all supported versions.

@eleftherias eleftherias added type: enhancement A general enhancement for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants