r/javahelp • u/jordaniansenpai • Sep 29 '20
Hey everyone we're using microservices for our upcoming project and I have a question about authentication
We use Spring Boot as our framework.
And I have a question that I'm unfamiliar with
---
Let's say user (x) wants to perform the following actions in our media service:
- Upload
- Download
- Delete file
---
Now we already have an authentication service that accepts Bearer token and return account info for the specific service.
But I feel it's bad code practice to copy-paste let's say the auth class and inject it then preform the authentication on the (Bearer token) for each method in our service,
---
So does anyone have any suggestions?
Maybe using AOP for each service to get that token and authenticate the user each time the user requested the service?
So basically the code works for now but I feel it's a really bad practice.
4
u/Jtonna Sep 29 '20
Essentially what I'm doing and what I've read that others do, is request forwarding.
The frontend client passes the Bearer Token along with the request to any microservice endpoint that needs auth.
The Controller that received that request then makes its own request to the Auth Microservice to verify the Token. This returns that users userid. I name this endpoint "/verify-token"
On GET Requests we can speed up this process by pre-emptively getting the data the client requested Before making the Token Verification Call & only returning it if the call was successful. The same technique can be used on all other requests except DELETE requests. You just CREATE / UPDATE the object/s and save them if the Token Verification was successful.
Sorry for formatting; on mobile.
5
u/Jtonna Sep 29 '20
Note: This can also be sped up further if needed by caching tokens & their userids that were sucesful. Just set expiration times on the cached object.
1
Sep 29 '20
As said before, doing it at the gateway is usually the best choice, since it sets a single place you need to change when you have to make any fix*.
If you need/have to do it on every single microservice, I think the next best choice would be to make a client library with the needed code and add it as a dependency, then you declare a bean and you use it as needed. But in this case, you need a sound strategy for versioning, since either you will have to update every place you declared the dependency on , or you will need to overwrite the version (I don't know how comfortable you are with that)
* Notice that it say when and not if
1
u/wildjokers Sep 29 '20
You should have an API gateway and that gateway should be making sure a user is authenticated. If and only if a user is authenticated should it ever forward requests on to a µservice.
If you have µservices that have unauthenticated endpoints (like the endpoints used to get authenticated in the first place) you will need an opt-in configuration to tell a gateway it should allow certain endpoints through.
1
u/daru567 Sep 29 '20
In practice, I have been using jwt tokens used at the header level. You can look into that.
1
8
u/King5lay3r Sep 29 '20
Do you have an API gateway in place for your microservices? Yes you can use AOP approach if its just a single service but I'd prefer looking into the Zuul filter. Basically you create a prefilter for your microservices and every request to your services gets through that prefilter.