Propagating User Identity

User Token or Service Token?

SituationSendWhy
order-service reads the user's addresses from user-servicethe user's tokenuser-service applies the user's permissions (only own addresses)
order-service charges a paymentservice token (client credentials)only order-service may charge; users must not call payments directly
nightly job recalculates seller ratingsservice tokenthere is no user
need both (who asked + which service)token exchange (RFC 8693)a new token with the user as sub and the service as actor

Forwarding the User's Token

@Bean
RestClient userServiceClient(RestClient.Builder builder) {
    ClientHttpRequestInterceptor bearerRelay = (request, body, execution) -> {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth instanceof JwtAuthenticationToken jwtAuth) {
            request.getHeaders().setBearerAuth(jwtAuth.getToken().getTokenValue());
        }
        return execution.execute(request, body);
    };
    return builder.baseUrl("http://user-service").requestInterceptor(bearerRelay).build();
}

Tip: The security context lives in a ThreadLocal. If you call user-service from @Async code or a CompletableFuture, wrap the executor with DelegatingSecurityContextExecutor so the token travels with the task.