gpt4 book ai didi

spring-cloud - Spring Cloud Gateway 注入(inject)头

转载 作者:行者123 更新时间:2023-12-04 23:37:13 27 4
gpt4 key购买 nike

在 GatewayFilter 中,我试图将 header 注入(inject)到请求中,如下所示。
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//DO SOME AUTHORIZATION OPERATIONS
String jwt = "xxxxx"
exchange.getRequest().getHeaders().add("headerkey", jwt);
return chain.filter(exchange);
}

但我收到以下错误:

java.lang.UnsupportedOperationException: null
at java.util.Collections$UnmodifiableMap.computeIfAbsent(Collections.java:1535) ~[na:1.8.0_111]
at org.springframework.http.HttpHeaders.add(HttpHeaders.java:1425) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at com.trimble.sample.springcloudgateway.filter.AuthGatewayFilter.filter(AuthGatewayFilter.java:30) ~[classes/:na]
at org.springframework.cloud.gateway.filter.OrderedGatewayFilter.filter(OrderedGatewayFilter.java:44) ~[spring-cloud-gateway-core-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.cloud.gateway.handler.FilteringWebHandler$DefaultGatewayFilterChain.filter(FilteringWebHandler.java:103) ~[spring-cloud-gateway-core-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.cloud.gateway.filter.NettyWriteResponseFilter.filter(NettyWriteResponseFilter.java:62) ~[spring-cloud-gateway-core-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.cloud.gateway.handler.FilteringWebHandler$GatewayFilterAdapter.filter(FilteringWebHandler.java:121) ~[spring-cloud-gateway-core-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.cloud.gateway.filter.OrderedGatewayFilter.filter(OrderedGatewayFilter.java:44) ~[spring-cloud-gateway-core-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]
at org.springframework.cloud.gateway.handler.FilteringWebHandler$DefaultGatewayFilterChain.filter(FilteringWebHandler.java:103) ~[spring-cloud-gateway-core-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT]

最佳答案

以下代码有效:

ServerHttpRequest request = exchange.getRequest()
.mutate()
.header("x-jwt-assertion", jwt)
.build();
ServerWebExchange exchange1 = exchange.mutate().request(request).build();
return chain.filter(exchange1);

关于spring-cloud - Spring Cloud Gateway 注入(inject)头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49845335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com