Working with Authentication in Microservices

1. Understanding Distributed Authentication

ChallengeDetail
No shared sessionStateless tokens preferred
Token validation costVerify locally with JWKS, not call IdP per request
Service identitySPIFFE/SPIRE, workload identity
Trust boundaryEdge auth vs internal

2. Implementing API Gateway Authentication

PatternDetail
Token verificationGateway verifies JWT, forwards claims
Token exchangeEdge token → internal short-lived token
Header injectionX-User-Id, X-Scopes (signed)
ToolsKong, Envoy, Apigee, AWS API Gateway

3. Using Service-to-Service Authentication

MechanismDetail
mTLSVia service mesh (Istio)
JWT-SVIDSPIFFE workload identity
Client credentialsOAuth 2.0 M2M
HMACPre-shared key signing

4. Implementing JWT Propagation

Example: Forward auth header

// Service A calling Service B - propagate user context
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", currentRequest.getHeader("Authorization"));
restTemplate.exchange(b_url, GET, new HttpEntity<>(headers), Response.class);

5. Using Service Mesh

FeatureDetail
mTLSAutomatic between pods (Istio Citadel)
AuthorizationPolicyL7 RBAC at sidecar
RequestAuthenticationJWT validation in Envoy
IdentitySPIFFE ID per pod

6. Implementing Token Validation in Services

ApproachDetail
Local JWKSCache keys; verify in-process
IntrospectionRFC 7662 — call IdP (slower; opaque tokens)
Trust chainValidate iss, aud, exp, signature
Librarynimbus-jose-jwt, jose, python-jose

7. Using Shared Authentication Service

TypeDetail
Centralized IdPKeycloak, Auth0, Okta, Cognito
Self-hostedOry Kratos/Hydra, ZITADEL, Authentik
BenefitOne place for users, MFA, sessions

8. Implementing mTLS Between Services

ApproachDetail
ManualIssue certs from internal CA
SPIREWorkload attestation + auto cert rotation
Cert-managerK8s CRDs for cert lifecycle
MeshAuto with Istio/Linkerd

9. Handling Authentication Context Propagation

ConcernDetail
HeadersAuthorization, X-Request-Id, traceparent (W3C)
Context objectPer-request, ThreadLocal/AsyncLocalStorage
AsyncPass explicitly through Promises/Reactor

10. Implementing Distributed Sessions

StoreDetail
RedisCluster mode, AOF, replication
DynamoDBTTL attribute auto-expires
JWTNo store, but revocation harder