简体   繁体   English

不同微服务中的授权检查

[英]Authorization check in different microservices

I decided to try for the first time to implement a microservice architecture instead of a monolithic one and ran into an authorization problem.我决定第一次尝试实现微服务架构而不是单体架构,但遇到了授权问题。 In a monolithic architecture, I simply passed the token in the header when accessing the controller on which the [Authorize] attribute was hanging and checked it against the current single database.在单体架构中,当访问 [Authorize] 属性挂起的 controller 时,我只是简单地传递了 header 中的令牌,并根据当前的单个数据库检查它。 But in the microservice architecture, each microservice has its own database, how you can check the token when accessing other microservices, I have heard about the implementation of the check in API Gateway, but I think that, anyway, each microservice should have its own check, since, there should be no access to the api if the user is not authorized.但是在微服务架构中,每个微服务都有自己的数据库,在访问其他微服务的时候怎么查token,我听说在API网关中实现了查,不过我觉得,反正每个微服务应该有自己的检查,因为如果用户未获得授权,则不应访问 api。 Should I use api gateway to make a request to the authorization microservice for verification?我应该使用 api 网关向授权微服务发出请求进行验证吗? How can I implement this?我该如何实施?

I have a separate microservice for user authorization (registration, login, issue of tokens) which has a database of users with tokens.我有一个用于用户授权(注册、登录、令牌发行)的单独微服务,它有一个带有令牌的用户数据库。 That is, I need to make a request to this microservice using API Gateway?也就是我需要使用API网关向这个微服务发出请求吗?

One way - You should try to do authentication/authorization at API Gateway level.一种方法 - 您应该尝试在 API 网关级别进行身份验证/授权。 Whenever any API call come to API Gateway that needs some permission then check the token.每当任何 API 调用来到需要一些权限的 API 网关时,请检查令牌。 If the access/token is not present then return 401. On frontend, if you get 401 then do authentication at UI.如果访问/令牌不存在,则返回 401。在前端,如果您得到 401,则在 UI 上进行身份验证。

2nd Way - UI pass token to API Gateway that will further send the token to other microservices.第二种方式 - UI 将令牌传递给 API 网关,该网关将进一步将令牌发送到其他微服务。

It depends on, how grain level of permission do you need.这取决于您需要多少粒度级别的权限。 If it is at very grain level, then go with 2nd else go with 1st.如果它处于非常颗粒级别,则 go 与第二个,否则 go 与第一个。

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

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