简体   繁体   English

管理RESTful服务的权限

[英]Managing permissions on RESTful service

I am working on an SPA application using angularjs and Web API. 我正在使用angularjs和Web API开发SPA应用程序。 I've been working on setting up user permissions for the resources on the api. 我一直在努力为api上的资源设置用户权限。

Since the api is a RESTful service, I'm not sure the best practice to store/retrieving the permissions. 由于api是RESTful服务,因此我不确定存储/检索权限的最佳实践。 The permissions in my application can change somewhat frequently, as users have different permissions for different companies they belong to. 我的应用程序中的权限可能会经常更改,因为用户对其所属的不同公司具有不同的权限。 The user can change their company on the fly in the application. 用户可以在应用程序中即时更改他们的公司。

The way I have is solved currently, is when the user logs in, the application stores a claim for every permission the user has, for all companies. 我目前解决的方法是,当用户登录时,应用程序为用户拥有的所有公司的所有权限存储声明。 The claim is stored with permission name and company id concatenated together. 声明与权限名称和公司ID串联在一起存储。 Then I have an attribute that accepts a permission name. 然后,我有一个接受权限名称的属性。 Then I concatenate that name with the selected company id in the app and see if it exists in the claims. 然后,我将该名称与应用中选定的公司ID串联在一起,然后查看声明中是否存在该名称。 If so, they have access. 如果是这样,他们就可以访问。

Another option I see is to only store only pertinent user info in claims (user id, name). 我看到的另一个选项是仅在声明中存储相关的用户信息(用户ID,名称)。 Then do a lookup every time I need roles or permissions. 然后在每次需要角色或权限时进行查找。 A drawback here is there will be a lot of traffic since I need that information on almost every api request. 这里的一个缺点是会有很多流量,因为我几乎每个api请求都需要该信息。 Also, since I have the authentication and resource server separated, it's not a simple lookup. 另外,由于我将身份验证和资源服务器分开,因此这不是一个简单的查询。 I'd have to go over http to get the data. 我必须翻阅http才能获取数据。

Are these my only options or is there a better way to handle this? 这些是我唯一的选择,还是有更好的方法来解决?

You want to use Bearer Tokens to solve this problem. 您想要使用承载令牌来解决此问题。 A bearer token is essentially an encrypted JSON string, that can contain all of the claims for a user. 承载令牌本质上是一个加密的JSON字符串,可以包含用户的所有声明。 In your case, I recommend a having separate claim for every company/permission combination. 对于您的情况,我建议对每个公司/权限组合单独提出索赔。

Bearer tokens are created on the server, and the exact technique you would use to generate one depends on the server technology/framework you are using. 承载令牌是在服务器上创建的,生成令牌的确切技术取决于您使用的服务器技术/框架。

Your SPA will send the bearer token with every request (take a look at $HttpInterceptor service to accomplish this). 您的SPA将在每次请求时发送承载令牌(请查看$ HttpInterceptor服务以完成此操作)。 At the server, your service will decrypt the token, and use the claims information therein to verify whether or not the user has permissions for the given endpoint. 在服务器上,您的服务将解密令牌,并使用其中的声明信息来验证用户是否具有给定端点的权限。

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

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