0

我在我的控制器端点中收到X-Forwarded-HostX-Forwarded-Proto,并且端点有一个反应管道来调用 ReactiveFeignClient 类。

这些标头应该传播到我的客户端请求,但正如我所见,它没有。我Principal在这个管道中没有,因为端点不需要身份验证,所以我不能使用ReactiveSecurityContextHolder.withAuthentication(user)

我已经添加了一个WebFilter从请求中读取标头:

    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;
        });
    }

但我不知道我可以在客户端的配置中从哪里检索它们并将它们Context放入客户端的请求中。

现在我这样做:(

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

和:

    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);
    }

但不转发任何标题。

4

1 回答 1

0

我最终创建了一个自定义类来实现Authentication并将这些字段作为元数据属性添加到其中;因为即使此端点不需要身份验证,也会收到与成员 ID 和其他身份验证信息相关的标头,因此我可以构造一个Authentication主体。

实际上,正如我所见,使用这个对象是唯一的方法。

于 2020-09-28T13:14:09.420 回答