springcloud gateway 跨域解决

package com.yhzj.gateway.config;

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.util.pattern.PathPatternParser;


/**
 * @author shupf
 */
@Configuration
public class CrossDomainConfig {

    /**
     * 解决跨域
     * @return
     */
    @Bean
    public CorsWebFilter  corsWebFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");

        config.setAllowCredentials(true); // 允许cookies跨域
        config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
        config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
        config.addAllowedHeader("*");// #允许访问的头信息,*表示全部
        config.addAllowedMethod("OPTIONS");// 允许提交请求的方法类型,*表示全部允许
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");

        config.addExposedHeader("set-cookie");
        config.addExposedHeader("access-control-allow-headers");
        config.addExposedHeader("access-control-allow-methods");
        config.addExposedHeader("access-control-allow-origin");
        config.addExposedHeader("access-control-max-age");
        config.addExposedHeader("X-Frame-Options");

        org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource source =
                new org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);
    }

}
发布了56 篇原创文章 · 获赞 86 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/qq_32331997/article/details/102697095