Currently I have a monolith application with Java/Spring Boot the following endpoints:
/login
/logout
/some-resource
To access some-resource
, the flow is following:
- The user makes a
POST
request to/login
endpoint. If the credentials are correct, a JWT token is returned in header, otherwise a 401. - The users sends the JWT token along with the request to
/some-resource
. If the token is valid, the resource is returned, otherwise 403.
Now I want to split the monolith into 2 services: "AuthServer" and "SomeResourceServer". There will be an API gateway on the top. I am thinking about 2 possible ways to handle authorisation
Option 1
- The user makes request to
/login
endpoint. The API gateway forwards it to the "AuthServer". If the credentials are correct, a JWT token is returned in header, otherwise a 401. - This step is the same - The users sends the JWT token along with the request to
/some-resource
. The API gateway calls the "AuthServer" to validate the JWT token. If the token is valid, the API gateway calls "SomeResourceServer" and returns the results. Otherwise 403.
Option 2
- The user makes request to
/login
endpoint. The API gateway forwards it to the "AuthServer". If the credentials are correct, a JWT token is returned in header, otherwise a 401. - This step is the same - The users sends the JWT token along with the request to
/some-resource
. The API gateway simply forwards the request to "SomeResourceServer". Then "SomeResourceServer" calls "AuthServer" to validate the JWT token. If the token is valid, the resource is returned, otherwise 403.
In Option 1 the API gateway is responsible to handle authorisation (communicate with "AuthServer"), in option 2 the communication is done between the servers. So which option is more correct? Are there any good/bad practices? Or maybe another way/option?