在微服务架构中组织授权的最佳实践?
Best practice to organize authorization in microservice architecture?
比如我有3个服务:
- 身份验证
- 卖家
- 买家
他们每个人都有自己的数据库、模型、服务...等等
身份验证服务了解用户、用户组、角色、权限并创建令牌。
我应该在哪里存储 sellers/buyers 个实体?在身份验证服务上,还是在 Seller/Buyer 服务上?
Seller/Buyer 服务应如何交互以创建新的 seller/buyer 实体?
Seller/Buyer 服务应该如何检查权限?
卖家和买家实体有一些共同的字段:姓名、密码、电子邮件...,但他们每个人都有自己的附加字段。
买卖双方互动。
我最近解决的一个问题听起来很熟悉
假设您的服务是基于 HTTP 的,那么我建议您查看 oAuth 2.0
来自 RFC 6749
的简短副本
OAuth addresses these issues by introducing an authorization layer
and separating the role of the client from that of the resource
owner. In OAuth, the client requests access to resources controlled
by the resource owner and hosted by the resource server, and is
issued a different set of credentials than those of the resource
owner.
Instead of using the resource owner's credentials to access protected
resources, the client obtains an access token -- a string denoting a
specific scope, lifetime, and other access attributes. Access tokens
are issued to third-party clients by an authorization server with the
approval of the resource owner. The client uses the access token to
access the protected resources hosted by the resource server.
For example, an end-user (resource owner) can grant a printing
service (client) access to her protected photos stored at a photo-
sharing service (resource server), without sharing her username and
password with the printing service. Instead, she authenticates
directly with a server trusted by the photo-sharing service
(authorization server), which issues the printing service delegation-
specific credentials (access token).
它只是将身份验证和授权建模为
之间的工作流
一个用户
- 拥有一些数据,因此也被称为资源所有者
- 有证书
授权服务器
- 拥有并控制用户身份、凭证和声明
- 控制授予和拒绝对用户资源的访问权限(在这种情况下并不真正需要)
- 将用户的凭据交换为 access_token,然后客户端可以使用该凭据访问来自资源提供者的信息
- 可选 授予一个 refresh_token 可用于续订过期的 access_token
资源提供者
- 有信息的服务
- 信任授权服务器
- 验证 access_token 有效(未过期、签名正确等)
- 验证所需声明是否存在(用户、角色等)
- 并向请求的客户发布信息
客户
- 一个应用程序(内部或第 3 方)
- 通过已知的授权服务器验证用户
- 获得一个access_token
- 使用access_token调用资源提供者获取信息
声明身份
声明身份 (explained better in more details here) 不仅仅是用户名和密码,它还可以为经过身份验证的用户携带许多声明,例如电子邮件、出生日期等,您可以使用这些声称将任何常见的用户属性传达给您的各种服务。
共享属性
现在,您的最后一个问题是关于将用户(或身份)链接到每个服务中的实体,该实体表示该服务上下文中的一些独特信息...这可以通过链接现有的经过身份验证的身份和 access_token 到每个服务中用户的内部表示。
类似于:
- 卖家即用户
- 买家即用户
- 一个用户有(声明,access_token)
- 声明是键值对
- 声明可以是(姓名、电子邮件、角色等)
比如我有3个服务:
- 身份验证
- 卖家
- 买家
他们每个人都有自己的数据库、模型、服务...等等
身份验证服务了解用户、用户组、角色、权限并创建令牌。
我应该在哪里存储 sellers/buyers 个实体?在身份验证服务上,还是在 Seller/Buyer 服务上?
Seller/Buyer 服务应如何交互以创建新的 seller/buyer 实体?
Seller/Buyer 服务应该如何检查权限?
卖家和买家实体有一些共同的字段:姓名、密码、电子邮件...,但他们每个人都有自己的附加字段。
买卖双方互动。
我最近解决的一个问题听起来很熟悉
假设您的服务是基于 HTTP 的,那么我建议您查看 oAuth 2.0
来自 RFC 6749
的简短副本OAuth addresses these issues by introducing an authorization layer and separating the role of the client from that of the resource owner. In OAuth, the client requests access to resources controlled by the resource owner and hosted by the resource server, and is issued a different set of credentials than those of the resource owner.
Instead of using the resource owner's credentials to access protected resources, the client obtains an access token -- a string denoting a specific scope, lifetime, and other access attributes. Access tokens are issued to third-party clients by an authorization server with the approval of the resource owner. The client uses the access token to access the protected resources hosted by the resource server.
For example, an end-user (resource owner) can grant a printing service (client) access to her protected photos stored at a photo- sharing service (resource server), without sharing her username and password with the printing service. Instead, she authenticates directly with a server trusted by the photo-sharing service (authorization server), which issues the printing service delegation- specific credentials (access token).
它只是将身份验证和授权建模为
之间的工作流一个用户
- 拥有一些数据,因此也被称为资源所有者
- 有证书
授权服务器
- 拥有并控制用户身份、凭证和声明
- 控制授予和拒绝对用户资源的访问权限(在这种情况下并不真正需要)
- 将用户的凭据交换为 access_token,然后客户端可以使用该凭据访问来自资源提供者的信息
- 可选 授予一个 refresh_token 可用于续订过期的 access_token
资源提供者
- 有信息的服务
- 信任授权服务器
- 验证 access_token 有效(未过期、签名正确等)
- 验证所需声明是否存在(用户、角色等)
- 并向请求的客户发布信息
客户
- 一个应用程序(内部或第 3 方)
- 通过已知的授权服务器验证用户
- 获得一个access_token
- 使用access_token调用资源提供者获取信息
声明身份
声明身份 (explained better in more details here) 不仅仅是用户名和密码,它还可以为经过身份验证的用户携带许多声明,例如电子邮件、出生日期等,您可以使用这些声称将任何常见的用户属性传达给您的各种服务。
共享属性
现在,您的最后一个问题是关于将用户(或身份)链接到每个服务中的实体,该实体表示该服务上下文中的一些独特信息...这可以通过链接现有的经过身份验证的身份和 access_token 到每个服务中用户的内部表示。
类似于:
- 卖家即用户
- 买家即用户
- 一个用户有(声明,access_token)
- 声明是键值对
- 声明可以是(姓名、电子邮件、角色等)