简体   繁体   English

使用Spring Security时获取当前登录的用户名

[英]Getting the current logged in user name when using Spring Security

Its a coupon system app using Spring security, spring MVC, now. 它是一个使用Spring Security Spring MVC的优惠券系统应用程序。 when the app starts, I need to somehow initialize the current logged in user into the controller. 当应用启动时,我需要以某种方式将当前登录的用户初始化到控制器中。

Issue is: 问题是:

  1. If I try to get the current user via SecurityContextHolder it is impossible because it seems like spring is initializing the controllers before the security so I cannot get it in the controller. 如果我尝试通过SecurityContextHolder获取当前用户,那是不可能的,因为似乎spring在安全性之前初始化了控制器,所以我无法在控制器中获取它。

Is there anything I'm missing? 有什么我想念的吗? a different approach of getting the current logged in user after he logs in? 在登录后获得当前登录用户的另一种方法?

What you need is called @AuthenticationPrincipal . 您需要的是@AuthenticationPrincipal You can inject it in controller method like this: 您可以将其注入控制器方法中,如下所示:

@GetMapping("/")
public void get(@AuthinticationPrincipal User user){ ... }

Here is documentation 这是文档

Alternatively, you can create your own annotation and custom argument resolver, and inject whatever you want. 或者,您可以创建自己的注释和自定义参数解析器,然后注入所需的任何内容。

Solution 1: Principal principal 解决方案1:校长

    @RequestMapping(value = {"/", ""})
    public String start(Principal principal, Model model) {
           String currentUser = principal.getName();       
           return currentUser;
    }

Solution 2: Authentication authentication 解决方案2:身份验证

@RequestMapping(value = {"/", ""})
public String currentUserName(Authentication authentication) {
        return authentication.getName();
}

Solution 3: SecurityContextHolder 解决方案3:SecurityContextHolder

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal instanceof UserDetails) {
  String username = ((UserDetails)principal).getUsername();
} else {
  String username = principal.toString();
}

More Here 这里更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取当前登录用户的spring + angular - Getting current logged user in spring + angular 检查当前用户是否通过Spring Security中的remember me过滤器登录 - Check if current user is logged in by the remember me filter in Spring Security 如何从spring security获取当前登录的用户对象? - How to get the current logged in user object from spring security? Spring Security:使用 UserDetailesService 时可以访问当前用户原则 - Spring Security : Could access to current user principle when using UserDetailesService 春季安全检查单击按钮时是否登录用户 - Spring security check the user is logged or not when click a button 即使用户登录,访问也被拒绝 spring 安全性 - Access is denied spring security even when user is logged in 使用 JavaFX 获取当前记录的用户 ID 和用户名 - Get Current logged User Id and User Name using JavaFX 如何使用Spring Security检查用户是否从另一个Web应用程序登录 - how to check if a user is logged in from another webapp using spring security 如何使用 Spring Security 获取用户在我的控制器中登录的角色 - How to get the role that a user logged in with in my controller using Spring Security 如何使用Spring Security 3.1更改当前用户的登录名? - How to change the login name for the current user with Spring Security 3.1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM