简体   繁体   English

如何访问 Java spring 中已登录用户的数据?

[英]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.

相关问题 如何在 Spring Boot Thymeleaf 中获取当前登录用户的数据? - How can I get the currently logged in user's data in Spring Boot Thymeleaf? 如何将已登录用户的会话数据传输到另一个Java应用程序 - How can i transfer session data of a logged in user to another Java application 我如何查看或隐藏一些数据,取决于是否登录的用户? - How can I view or hide some data depending of user logged in or not? 在Spring 3控制器中,如何检查登录用户的权限并有条件地执行某些操作? - In a Spring 3 controller, how can I check the permissions of the logged-in user and do certain actions conditionally? 如何在Java的Spring Aop logger类内的会话中访问任何数据? - How can i access any data in session inside spring aop logger class in java? 无法访问Spring Security登录的用户信息 - Not able to access Spring Security Logged in User info Java Spring 安全性。 记录的用户信息 - Java Spring Security. Logged user information 登录用户的不同视图(Spring,java,thymeleaf) - Different view by logged user (Spring, java, thymeleaf) 用户登录后,如何在控制器中获取会话数据 - How can I get session data in my controller after user is logged in java spring:如何区分用户角色和权限? - java spring : how can I separate user roles and authorities?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM