r/SpringBoot • u/Gold_Opportunity8042 • Mar 08 '26
Discussion Should i create two seperate controller for internal endpoints and public endpoints?
Hey!!
I am creating a java spring boot microservice project. The endpoints are classified into two category :
- called by the external user via api-gateway.
- service-to-service called apis.
My question is, from the security point of view should i create two separate controller : one for external apis and another for internal service-to-service apis and block the internal endpoints called from api-gateway? What usually is the industry standard?
Appreciate if someone can share their knowledge on this.
Thank you!!
23
Upvotes
28
u/iLoveCalculus314 Mar 08 '26
Your controllers should be separated by domain, not by access rules.
1
u/leetjourney Mar 13 '26
You shouldn't really need a controller for "internal" endpoints, those would simply be calls between service classes as you said. Controllers should really be separated by feature/domain
6
u/manu_moreno Mar 08 '26 edited Mar 08 '26
You should consider separating your functionality into 2 different modules.
Example:
This approach follows the Unix design philosophy -- do one thing and do it well. Also security becomes easier in that you can apply most security rules to your rest-api in order to protect your data and other back-end resources. You can then use JWT tokens to allow the front-end to access the back-end.
In fact, I'm developing a SpringBoot app which implements this very design. I have a 3rd module (common-lib), which hosts reusable artifacts.
1
u/BikingSquirrel Mar 08 '26
Yes, sounds like a good idea.
Mainly because it's easier and more obvious to separate the paths of their URLs. Which then makes it easier to configure the access.
1
u/WVAviator Mar 09 '26
We do this in one of our microservices. We have
/api/v1/...and/external/v1/...endpoints that sit in different controllers. We took it a step further though and they also have different DTOs for request and response objects, as well as different services (but usually just different methods on the same service) even though they are serving and persisting the same underlying resource through the same repository. There's more validation on the external endpoints since it's more likely to receive bad data, and we want internal users to be able to override some of those restrictions anyway. We also have separate swagger docs as well, one with all external endpoints and the other with the internal ones. This way we don't expose any other information about our API.