• 跨域问题以及经过网关二次转发重复跨域


    1跨域问题解决

    package com.win.credit.point.gateway.config;

    import java.time.Duration;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.cors.CorsConfiguration;
    import org.springframework.web.cors.reactive.CorsWebFilter;
    import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
    import org.springframework.web.util.pattern.PathPatternParser;
    /**
     */
    @Configuration
    public class CorsConfig {
        @Bean
        public CorsWebFilter corsFilter() {
            CorsConfiguration config = new CorsConfiguration();
            config.addAllowedMethod("*");;
            config.addAllowedOrigin("*");
            config.addAllowedHeader("*");
            config.setMaxAge(Duration.ofHours(24));//options重复请求cache优化
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
            source.registerCorsConfiguration("/**", config);

            return new CorsWebFilter(source);
        }
    }

    2有网关一层,网关会转发一次,重复跨域

    package com.win.credit.point.gateway.filter;
    import java.util.Arrays;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.cloud.gateway.filter.GatewayFilterChain;
    import org.springframework.cloud.gateway.filter.GlobalFilter;
    import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
    import org.springframework.core.Ordered;
    import org.springframework.http.HttpHeaders;
    import org.springframework.stereotype.Component;
    import org.springframework.web.server.ServerWebExchange;

    import reactor.core.publisher.Mono;
    /**
     * 重复跨域,去重
     */
    @Component("corsResponseHeaderFilter")
    public class ResponseCorsHeaderFilter implements GlobalFilter, Ordered {

        private static final Logger logger = LoggerFactory.getLogger(ResponseCorsHeaderFilter.class);
        
        @Override
        public int getOrder() {
            // 指定此过滤器位于NettyWriteResponseFilter之后
            // 即待处理完响应体后接着处理响应头
            return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER + 1000;
        }

        /**
         * Process the Web request and (optionally) delegate to the next {@code WebFilter}
         * through the given {@link GatewayFilterChain}.
         *
         * @param exchange the current server exchange
         * @param chain    provides a way to delegate to the next filter
         * @return {@code Mono} to indicate when request processing is complete
         */
        @Override
        public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                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(Arrays.asList(kv.getValue().get(1)));
                });
            }));
        }
    }
     

    本文只给解决方式,仅供参考。

  • 相关阅读:
    【C/PTA】数组练习(编程)
    GraphRAG学习小结(4)
    EtherCAT从站转modbus RTU协议转换网关用modbus slave测试的方法
    企业邮箱认证指南:安全与高效的邮箱认证方法
    领悟《信号与系统》之 非周期信号的傅里叶变换及性质
    HTML+CSS大作业 格林蛋糕(7个页面) 餐饮美食网页设计与实现
    产品评论观点提取Baseline-2021 CCF BDCI 数据挖掘 top3方案分享 数据+代码
    Nginx 40 问!
    浅谈K-means算法和实现(基于Python)
    我是如何将一个老系统的kafka消费者服务的性能提升近百倍的
  • 原文地址:https://blog.csdn.net/guanfengliang1988/article/details/126798349