'How can I access data of a logged in user in Java spring?
So I have Users and Some data of those users. I want that logged in user can access only to his data. So If two users are logged in they access only to their data.
Is that feasibile with SessionRegistry? but how should be the API calls?
When user logs in I put this in the controller:
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(
user.getMail(),
user.getPassword(),
userDetails.getAuthorities()));
Solution 1:[1]
Spring manages user information in SecurityContextHolder. So, you can access user information anytime from the securityContextHolder. This is how it is done :
Users user = (Users) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
user will contain necessary information.
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 | Amimul Ehsan Rahi |
