Spring 登录后显示用户详细信息
我正在为我的web应用程序使用Spring security,我想显示一些登录用户的详细信息。在我的代码中,我尝试在登录后在用户本身上使用@Autowired,并将其用作模型,以在前端显示其某些字段:Spring 登录后显示用户详细信息,spring,spring-boot,spring-security,Spring,Spring Boot,Spring Security,我正在为我的web应用程序使用Spring security,我想显示一些登录用户的详细信息。在我的代码中,我尝试在登录后在用户本身上使用@Autowired,并将其用作模型,以在前端显示其某些字段: @Autowired public AttractionController(XyService xyService, ApplicationUser applicationUser) { this.xyService= xyService; this.applicati
@Autowired
public AttractionController(XyService xyService,
ApplicationUser applicationUser) {
this.xyService= xyService;
this.applicationUser = applicationUser;
}
@GetMapping("/")
String main(Model model, @ModelAttribute
Attraction xy) {
model.addAttribute("xy", xyService.findAll());
model.addAttribute("user", applicationUser);
return "main";
我认为问题不仅在于实施,而且在于方法本身
我应该如何解决这个问题
提前谢谢。假设
ApplicationUser
是一种保存用户信息的数据类,自动连接它完全没有意义,因为它更像是不受Spring控制的
为了获得当前经过身份验证的用户,只需使用SecurityContextHolder
。可以通过以下方式对其进行实例化:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// check if there is somebody authenticated
if (!(authentication instanceof AnonymousAuthenticationToken)) {
String currentUserName = authentication.getName();
// do something the the username, maybe get more information from a database
}
您使用的是哪种安全性:基本身份验证、oauth1、oauth2等?谢谢您的提问!我使用的是基本身份验证。哪些代码不适用?有错误信息吗?显示您的
ApplicationUser
代码。您只需要spring管理的用户信息还是需要广泛的用户信息:用户名、姓氏、地址、生日等?