简体   繁体   English

Spring Security REST API自定义JWT身份验证

[英]Spring security REST API custom JWT authentication

I am trying to create an API with JWT authentication with Spring security. 我正在尝试通过具有Spring安全性的JWT身份验证创建一个API。

Because it's an API I don't need all login/logout functionality from Spring security. 因为它是一个API,所以我不需要Spring安全性的所有登录/注销功能。 I just need the part where Spring Security determines the current user. 我只需要Spring Security确定当前用户的部分。

So my question is, where can I set the current user for each request? 所以我的问题是,在哪里可以为每个请求设置当前用户?

You can add Principal to your methods in your REST controllers. 您可以在REST控制器中将Principal添加到您的方法中。 The Principal contains the all the details of the API client identified by the token. 委托人包含令牌标识的API客户端的所有详细信息。 It includes client name, token details such as roles, scopes, and etc... 它包括客户端名称,令牌详细信息,例如角色,范围等。

    import java.security.Principal;

   @GetMapping(value = "/hello")
   public String whoAmI(Principal principal)  {
      return principal.getName();
   }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM