用户登录(ID),在后面。Jhipster 4.1.1
在使用Mongodb的Jhipster 4.4.1应用程序中,JWT。 我需要用户登录以进行查询,但我不知道如何在java控制器(资源)中检索它 在SecurityUtils中,我看不到如何获取ID用户登录(ID),在后面。Jhipster 4.1.1,jhipster,Jhipster,在使用Mongodb的Jhipster 4.4.1应用程序中,JWT。 我需要用户登录以进行查询,但我不知道如何在java控制器(资源)中检索它 在SecurityUtils中,我看不到如何获取ID public static String getCurrentUserLogin() { SecurityContext securityContext = SecurityContextHolder.getContext(); Authentication authenticati
public static String getCurrentUserLogin() {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
String userName = null;
if (authentication != null) {
if (authentication.getPrincipal() instanceof UserDetails) {
UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
userName = springSecurityUser.getUsername();
} else if (authentication.getPrincipal() instanceof String) {
userName = (String) authentication.getPrincipal();
}
}
return userName;
}
我可以捕获登录的用户ID,而无需咨询数据库
谢谢如果您需要当前用户,那么您可以根据问题中的
getCurrentUserLogin()
查询登录名
,因为它仍然是
如果你真的想要这个ID,那么随便看看,你会发现
UserDTO
和User
有一个.getID()
方法。如果你需要当前用户,那么你可以查询login
,正如getCurrentUserLogin()
在你的问题中给出的那样,因为不管怎么说,它是
如果你真的想要这个ID,那么仔细看看,你会发现
UserDTO
和User
有一个.getID()
方法。对于我来说,在我的资源中,这行代码允许我获取当前登录用户的ID。我只是不能100%确定这是否是正确的方法:Long id=userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin()).get().getId();在我的资源中,这行代码允许我获取当前登录用户的ID。我只是不能100%确定这是否是正确的方法:Long id=userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin()).get().getId();