简体   繁体   English

使用oAuth2保护Spring Boot API | 过滤用户

[英]Securing Spring Boot API with oAuth2 | filter users

I have a Spring boot API that I managed to secure with oAuth2 and Google. 我有一个Spring Boot API,可以通过oAuth2和Google进行保护。 I have something similar to this: Rest, Spring own OAuth2 server + OAuth2 providers like Facebook, Google, Yahoo 我有类似的东西: 休息,Spring拥有OAuth2服务器+ OAuth2提供程序,例如Facebook,Google,Yahoo

Everything works as expected. 一切正常。

My question is the following: How can I know limit the access to certain users (not every google user) ? 我的问题如下:我怎么知道将访问权限限制为某些用户(不是每个Google用户)? This sounds like the authorization part to me. 这听起来像是我的授权部分。 I'm not sure where to start as I'm a beginner in all this. 我不知道从哪里开始,因为我是这方面的初学者。

Thanks for any help or pointer. 感谢您的帮助或指导。

To the best of my knowledge, if you want to limit access to an API based on a user's last name (going by your example) I will set up a sample scenario of something I might do. 据我所知,如果您想基于用户的姓氏来限制对API的访问(以您的示例为例),我将设置一个可能的操作示例场景。

Say for instance I have an API with an endpoint such as /api/family?lastname=doe and the purpose of this API is to return a list of people with the same last name as "doe" in this case. 假设我有一个带有端点的API,例如/api/family?lastname=doe ,在这种情况下,此API的目的是返回一个姓氏与“ doe”相同的人。 The API will only return a result if the last name provided in the parameter is equal to the current authorized user's last name. 仅当参数中提供的姓氏等于当前授权用户的姓氏时,API才会返回结果。

So maybe I'd have an API set up as follows: 因此,也许我会按如下所示设置API:

@RequestMapping(value="/api/family", method = RequestMethod.GET)
@ResponseBody
public List<Member> findFamilyMembersByLastName(@RequestParam(value="lastname", required=true) String lastName){

      User authorizedUser = getCurrentUser(); // Set up a method that is able to get the current logged in user
      if(!authorizedUser.getLastName().equals(lastName){
         // Throw some kind of exception here
      }
      // otherwise perform a query to find a list of Members with last name and return them

}

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

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