I have implemented OAuth2 password grant with spring security module. I add own implementation of UserDetails and UserDetailsService (jdbc). I inject User to my controllers with:
@AuthenticationPrincipal User user
where User is my implementation of UserDetails . Now I want to add posibility of changing User data without refreshing token.
I try to refresh principals with:
User updatedUser = ...
Authentication newAuth = new UsernamePasswordAuthenticationToken(updatedUser, updatedUser.getPassword(), updatedUser.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(newAuth);
But it doesn't work, when I invoke another controller method it returns old User object.
Is there any way to change User data without refreshing token? Is any solution to make spring security to always load user data from database (not from Cache)?