[英]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? SessionRegistry 是否可行? but how should be the API calls?
但是API应该怎么调用呢?
When user logs in I put this in the controller:当用户登录时,我把它放在 controller 中:
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(
user.getMail(),
user.getPassword(),
userDetails.getAuthorities()));
Spring manages user information in SecurityContextHolder. Spring 管理SecurityContextHolder中的用户信息。 So, you can access user information anytime from the securityContextHolder.
因此,您可以随时从 securityContextHolder 访问用户信息。 This is how it is done:
这是如何完成的:
Users user = (Users) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
user will contain necessary information.用户将包含必要的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.