ReactiveFeignClient - how to propagate headers from controller to client without auth

1.1k views Asked by At

I received X-Forwarded-Host and X-Forwarded-Proto in my controller endpoints, and the endpoint has a reactive pipeline to call a ReactiveFeignClient class.

These headers should be propagated to my client requests, but as I see it, it has not. I have no Principal in this pipeline, because the endpoints needs no auth, so I cannot use ReactiveSecurityContextHolder.withAuthentication(user)

I already added a WebFilter to read headers from request:

    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        return chain.filter(exchange).subscriberContext((context) -> {
            ServerHttpRequest request = exchange.getRequest();
            Map<String, String> headers = (Map)request.getHeaders().toSingleValueMap().entrySet().stream().filter((entry) -> {
                return ((String)entry.getKey()).equalsIgnoreCase(this.authLibConfig.getXForwardedHostHeader()) || ((String)entry.getKey()).equalsIgnoreCase(this.authLibConfig.getXForwardedProtoHeader());
            }).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
            System.out.println("Adding all headers now: ");
            context.put("headers_to_propagate", headers);
            return context;
        });
    }

But I don't know where in the config of client can I retrieve them from the Context and put into requests in client.

Now I do this:(

    @Bean
    public ReactiveHttpRequestInterceptor forwardingHeadersInterceptor(ReactiveFeignUtils reactiveFeignUtils) {
        return reactiveFeignUtils::mutateRequestHeadersForNoAuthRequests;
    }

And:

    public Mono<ReactiveHttpRequest> mutateRequestHeadersForNoAuthRequests(ReactiveHttpRequest reactiveHttpRequest) {
        return Mono.subscriberContext().doOnNext((context) -> {
            System.out.println("Current context: " + context.toString());
            if (context.hasKey("headers_to_propagate")) {
                System.out.println("Getting all host headers: ");
                reactiveHttpRequest.headers().putAll((Map)context.get("headers_to_propagate"));
            }

        }).thenReturn(reactiveHttpRequest);
    }

But no headers are forwarded.

1

There are 1 answers

0
WesternGun On

I ended up creating a customized class implementing Authentication and add these fields as metadata property to it; because even though this endpoint requires no auth, headers related to member id and other auth info are received, so I can construct an Authentication principal.

Actually as I see, working with this object is the only way.