Access Token/Authorization Between Microservices

Viewed 3881

I'm creating an online store REST API that will mainly be used by a mobile app. The plan is for a microservices architecture using the Spring Cloud framework and Spring Cloud OAuth for security.

My question is really on best practices for communication between microservices: Should I have each service register for their own token, or should they just pass the user's token around?

For example, I have 3 services: user-service, account-service, order-service. I've been able to implement two procedures for creating an order: One passes the user's token around, and in the other each service gets their own token. I use Feign for both approaches.

So for option 1: order-service -> GET account-service/account/current

order-service calls the account-service which returns the account based on a userId in the token. Then the order-service creates an order for the account.

Or for option 2: order-service -> GET account-service/account/user-id/{userId}

order-service gets the userId from the sent token, calls the account-service with it's own token, then creates the order with the retrieved account.

I'm really not sure which option is best to use. One better separates information but then requires two Feign Clients. However the other doesn't require the 2 clients and it becomes easier to block off end certain endpoints to outside clients, however it requires extra endpoints to be created and almost every service to go digging into the Authentication object.

What are all your thoughts? Has anyone implemented their system in one way or another way entirely? Or perhaps I've got the completely wrong idea.

Any help is appreciated.

3 Answers
Related