package com.games.gateway.filters;

import com.games.gateway.config.FilterOrderedConfig;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 用于 remove多的 Access-Control-Allow-Origin
 * 兼容底层里面 加了多的 Access-Control-Allow-Origin
 */
@Order(FilterOrderedConfig.ResponseHeaderModifyFilter)
@Component
public class ResponseHeaderModifyFilter implements GlobalFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        return chain.filter(exchange).then(Mono.fromRunnable(() -> {
            exchange.getResponse().getHeaders().remove("Access-Control-Allow-Origin");
            exchange.getResponse().getHeaders().add("Access-Control-Allow-Origin", "*");
        }));
    }
}
