Laravel passport, Oauth and microservices

戏子无情 提交于 2019-11-30 20:04:35

The common approach in microservices architecture is to use a single authentication 'gateway', and usually it's a part of an API gateway.

So besides your passport.ms.com, you have somewhat of a proxy that will check access token from the header and if it's invalid - give an error. If the token is valid - proxy the request to corresponding microservice.

This way you don't have to repeat yourself - you don't have to implement authentication N times for each microservice.

Then, if you need more granular control - what exactly a user can access (usually called authorisation), then you traditionally implement it at each specific microservice.

In short, your microservices shouldn't care if the incoming request is authenticated - it's already been pre-filtered for them. Microservices only decide whether the user X can do action Y.

PS. You can combine API gateway with Passport/Oauth facility or you may run them separately - that's up to you. AWS already offers API gateway as a service (proving how trendy microservices are becoming) but I couldn't find any good open source analogues.

Your api should have a gateway that handles the Authentication and communicates to different micro-services. Its makes sense to authenticate (or reject unauthorised) users at the top level, combine responses from different services and then your clients(Web or mobile) can consume that data.

An advantage of this is that your clients only need to remember just one url.

Example: Only microservice.com is needed and not catalog.microservice.com, users.microservice.com, passport.microservice.com etc.

A single endpoint address (URL) is much easier to remember and configure than many individual API addresses.

Here is a link to an image describing this architecture.

Api Architecture image

I'm no expert but that flow looks okay to me if you are required to use different applications for this.

Regarding your second question, yes this can be middleware

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!