Why Token Passing Is a Bad Idea and Better Alternatives for Microservice Calls

This article critiques the common practice of token passthrough for microservice authentication, explains why it hampers design and code reuse, and compares several internal call strategies—including Feign, Dubbo, Spring Boot Web with Dubbo, and K8s‑integrated approaches—highlighting their pros, cons, and best‑practice recommendations.

Architect
Architect
Architect
Why Token Passing Is a Bad Idea and Better Alternatives for Microservice Calls

Token Passthrough (Not Recommended)

When first learning microservices, many solutions use token passthrough for authentication, but this design is problematic.

Reasons: mixing internal and external APIs makes distinction difficult; internal APIs should be stateless to ensure atomicity and improve code reuse.

Instead of relying on the token to identify the user, the first service (gateway) should parse the token, extract the userId, and pass it explicitly to downstream services.

Example scenarios: user sign‑in adds points, admin manually adds points, batch job adds points. If the API derives the userId from the token, the admin scenario requires a separate API, reducing reuse.

Do not pass data implicitly; provide explicit parameters.

Implementation: the gateway parses the token, forwards userId in request headers, and downstream services use the provided userId without re‑parsing the token.

Note: internal APIs must not be exposed to the external network; use a path rule such as /api/inside/** and block external access at the gateway.

image.png
image.png

Unified Authorization

Centralize API authentication at the application gateway, which validates the token and forwards authenticated information (e.g., userId) to downstream services.

Feign Internal Call Method

Spring Cloud Gateway + Feign internal calls perform unified authentication at the gateway and add authenticated information to request headers for downstream services.

Drawback: Service B must expose a separate internal controller for Feign to call, increasing code volume.

image.png
image.png

Dubbo Internal Call Method

Spring Cloud Gateway + Dubbo internal calls also centralize authentication at the gateway and forward user information.

Advantages: No extra controller is needed; only local service and remote Dubbo service differ, making code simpler.

Drawback: Slightly increases the complexity of the technology stack.

image.png
image.png

Spring Boot Web + Dubbo Internal Call Method

This design removes the gateway and uses a Spring Boot Web project as a replacement. It should run on Undertow (a non‑blocking container) for performance.

All service controllers are integrated into the Web application, which handles unified authentication and authorization. Each service can provide its own controller module, and the gateway project only contains the startup class.

Pros: Simplifies project structure and avoids gateway thread‑pool considerations.

Cons: Cannot dynamically adjust routing via a configuration center; adding a new service requires redeployment.

image.png
image.png

Non‑Unified Authorization

Each service implements its own authentication, or a shared JAR can provide a common auth module.

Regular Mode

A generic authentication module can be shared across services, offering JWT parsing, permission interception, and caching (local or Redis).

This mode suits large teams where each microservice is owned by a separate group, allowing each to maintain its own permission rules while sharing the same logic.

image.png
image.png

Integration with Kubernetes

Replace the application gateway with a Kubernetes Ingress gateway, removing the need for a separate registration center. Services can be accessed directly via K8s Service names, e.g., http://goods-svc:8080/api/goods/info/10001 or dubbo://goods-svc:20880.

This reduces deployment complexity and eliminates an extra forwarding layer.

image.png
image.png

There is no universally correct solution; choose the approach that best fits your project.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

feignAuthenticationgateway
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.