[英]Securing Spring Boot API with oAuth2 | filter users
我有一个Spring Boot API,可以通过oAuth2和Google进行保护。 我有类似的东西: 休息,Spring拥有OAuth2服务器+ OAuth2提供程序,例如Facebook,Google,Yahoo
一切正常。
我的问题如下:我怎么知道将访问权限限制为某些用户(不是每个Google用户)? 这听起来像是我的授权部分。 我不知道从哪里开始,因为我是这方面的初学者。
感谢您的帮助或指导。
据我所知,如果您想基于用户的姓氏来限制对API的访问(以您的示例为例),我将设置一个可能的操作示例场景。
假设我有一个带有端点的API,例如/api/family?lastname=doe
,在这种情况下,此API的目的是返回一个姓氏与“ doe”相同的人。 仅当参数中提供的姓氏等于当前授权用户的姓氏时,API才会返回结果。
因此,也许我会按如下所示设置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.