'Pass of user details from Spring security config to Controller Class

Suggestion required after authentication from security config needs to pass the every user details to the APIs request.



Solution 1:[1]

The idea is that you can customise your authentication process such that after successful authentication , it will store the user object inside Authentication which can be retrieved by calling Authentication.getPrincipal().

Then in the controller method , you can access the user object representing the current user by :

//You have cast the user to the type of the user that you customzie manually   
MyCustomUser user = (MyCustomUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

Or use @AuthenticationPrincipal to access it :

@GetMapping("/foo")
public String getFoo(@AuthenticationPrincipal MyCustomUser user) {

    
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ken Chan