gpt4 book ai didi

spring - 如何在 Spring WebFilter 中添加自定义 header ?

转载 作者:行者123 更新时间:2023-12-02 07:03:03 24 4
gpt4 key购买 nike

我尝试在调用 REST 服务之前添加自定义过滤器。在下面的类中,我尝试在 HttpRequest 中添加自定义过滤器,但出现错误:-

java.lang.UnsupportedOperationException:null 在 java.util.Collections$UnmodifyingMap.computeIfAbsent(Collections.java:1535) ~[na:1.8.0_171] 在 org.springframework.util.CollectionUtils$MultiValueMapAdapter.add(CollectionUtils.java:459) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]

public class AuthenticationWebFilter implements WebFilter {
private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationWebFilter.class);

@Autowired
private TokenServiceRequest tokenServiceRequest;

@Autowired
private AuthenticationProvider authenticationProvider;

public AuthenticationWebFilter(TokenServiceRequest tokenServiceRequest, AuthenticationProvider authenticationProvider) {
super();
this.tokenServiceRequest = tokenServiceRequest;
this.authenticationProvider = authenticationProvider;
}

@Override
public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
HttpHeaders requestHeaders = serverWebExchange.getRequest().getHeaders();
HttpHeaders responseHeaders = serverWebExchange.getResponse().getHeaders();
LOGGER.info("Response HEADERS: "+responseHeaders);
LOGGER.info("Request HEADERS: "+serverWebExchange.getRequest().getHeaders());

tokenServiceRequest.setUsername(serverWebExchange.getRequest().getHeaders().getFirst(CommerceConnectorConstants.USERNAME));
tokenServiceRequest.setPassword(serverWebExchange.getRequest().getHeaders().getFirst(CommerceConnectorConstants.PASSWORD));
tokenServiceRequest.setClientId(serverWebExchange.getRequest().getHeaders().getFirst(CommerceConnectorConstants.CLIENT_ID));
tokenServiceRequest.setSecretClient(serverWebExchange.getRequest().getHeaders().getFirst(CommerceConnectorConstants.SECRET_CLIENT));
LOGGER.info("Token Received: " + authenticationProvider.getUserAccessToken(tokenServiceRequest).getTokenId());

//responseHeaders.set(CommerceConnectorConstants.X_AUTH_TOKEN, authenticationProvider.getUserAccessToken(tokenServiceRequest).getTokenId());
//responseHeaders.add(CommerceConnectorConstants.X_AUTH_TOKEN, authenticationProvider.getUserAccessToken(tokenServiceRequest).getTokenId());

//This below code is not working
serverWebExchange.getRequest().getQueryParams().add("test", "value");

//This below code is not working
//serverWebExchange.getRequest().getHeaders().add(CommerceConnectorConstants.X_AUTH_TOKEN, authenticationProvider.getUserAccessToken(tokenServiceRequest).getTokenId());
LOGGER.info("Exiting filter@AuthenticationWebFilter");
return webFilterChain.filter(serverWebExchange);
}
}

在 HTTPResponse 中,我可以设置自定义 header ,但我的要求是在 HTTPRequest 中添加自定义 header 。请指教。

最佳答案

如果您使用的是 Spring Cloud Gateway,则可以通过实现 GlobalFilter 或 GatewayFilter 来修改请求 header 。

    @Component 
public class LogFilter implements GlobalFilter, Ordered {

private Logger LOG = LoggerFactory.getLogger(LogFilter.class);

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

return chain.filter(
exchange.mutate().request(
exchange.getRequest().mutate()
.header("customer-header", "customer-header-value")
.build())
.build());
}

@Override
public int getOrder() {
return 0;
} }

如果您在 ZuulFilter 中,addZuulRequestHeader 可以修改请求 header 。

    RequestContext.getCurrentContext().addZuulRequestHeader("customer-header", "customer-header-value");

希望对您有帮助。

关于spring - 如何在 Spring WebFilter 中添加自定义 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50927662/

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