Closed sergio11 closed 7 years ago
This issue is because DelegatingFlashMessagesConfiguration
eagerly initializes the handlerExceptionResolver
which prevents the AuthenticationPrincipalArgumentResolver
from being registered until after Spring MVC has constructed the DispatcherServlet
s custom argument resolvers.
DelegatingWebMvcConfiguration
the container finds all the WebMvcConfigurer
. Then DelegatingFlashMessagesConfiguration
is created but it requires the handlerExceptionResolver
which adds all the argument resolvers before WebMvcSecurityConfiguration
is added as a WebMvcConfigurer
.
If you comment out the @EnableFlashMessages
it resolves this problem.
Summary
I have a Spring MVC controller where I save the post information associated with the currently authenticated user. This current user is obtained by using the @AuthenticationPrincipal annotation in the method next to the User class that implements the UserDetails interface. The problem is that this user has all the empty fields, however if I get the user through SecurityContextHolder this does not happen.
Actual Behavior
When the controller is executed the user obtained by
@AuthenticationPrincipal
is empty and the one obtained bySecurityContextHolder
contains all the information:Expected Behavior
As I understand using @EnableWebSecurity already enable in the context the resolver argument for the @AuthenticationPrincipal.
So I could use this to get the current user managed:
@AuthenticationPrincipal(expression = "@jpaEntityManager.merge(#this)")
And to be able to persist the Post associated with the current user
Configuration
Version
Spring Security 4.2.0.RELEASE
Sample
Github Repository
Stack Overflow