SpringGateway跨域请求头重复处理过滤器

编程

@Component

public class CorsResponseHeaderFilter implements GlobalFilter, Ordered {

@Override

public int getOrder() {

// 指定位于 NettyWriteResponseFilter 处理完响应体后移除重复 CORS 响应头

return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER + 1;

}

@Override

@SuppressWarnings("serial")

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

return chain.filter(exchange).then(Mono.defer(() -> {

exchange.getResponse().getHeaders().entrySet().stream()

.filter(kv -> (kv.getValue() != null && kv.getValue().size() > 1))

.filter(kv -> (kv.getKey().equals(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)

|| kv.getKey().equals(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS)))

.forEach(kv -> kv.setValue(new ArrayList<String>() {{

add(kv.getValue().get(0));

}}));

return chain.filter(exchange);

}));

}

}

以上是 SpringGateway跨域请求头重复处理过滤器 的全部内容, 来源链接: utcz.com/z/517122.html

回到顶部