-
Notifications
You must be signed in to change notification settings - Fork 6k
AuthenticationPrincipal argument type cannot be primitive #10172
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
Comments
Okay, perhaps I might have been asking too much to Spring. Here is another part of the analysis. Spring Security obviously uses SpEL to resolve expressions. At ReflectivePropertyAccessor I found that the I have debugged it and the return type is correctly Possible solutions Documenting that @AuthenticationPrincipal does not work with Java primitive types because of SpEL limitations? Feature-requesting SpEL to handle the cast to a primitive type? I believe discussion is now open |
Thanks for reaching out @djechelon. The argument type cannot be a primitive, since the I've created a pull request, gh-10213, to include that in the documentation. |
Describe the bug
I discovered an issue in the @AuthenticationPrincipal annotation handler
I have a custom
UserDetails
sub-interface displaying auserId
attribute ofint
type, corresponding to the user's primary key in the database. The primary key is always present if there is a principal, so it's type int everywhere.I retrieve it using @AuthenticationPrincipal in my REST controllers
The problem is that the
AuthenticationPrincipalArgumentResolver
does not correctly resolve int, a null argument is derived and is causing an unclear exception at controller invocation.The exception does not clearly indicate the problem and I had to debug deep into Spring to find out.
Code fragment from Spring Security:
At line 115, the
principal
object is set to an Integer, but at line 117 the cast fails (because, manifestly,int
cannot be assigned from an integer), but sinceerrorOnInvalidType
is false by default, the returned argument is null.The argument is then passed to the MVC method argument resolver, which cannot invoke the REST method
doSomething
but can't explain why exactlyThis is the stack trace
To Reproduce
Try to invoke an MVC method that takes an
int
parameter as part of the @AuthenticationPrincipal annotation mechanism. Maybe works with other primitive typesExpected behavior
Sample
Not yet available. It takes me a few hours to push a test repository, could the Spring team kindly assess the issue first?
It is possible for me to build a very basic Spring Boot project with a mock User Details test
The text was updated successfully, but these errors were encountered: