zoukankan      html  css  js  c++  java
  • 网关解决跨域问题,spring-cloud-gateway zuul

    1.跨域:对于一个路径:http:ip:port 如果协议 ip 端口三者有一个不同就有可能产生跨域问题

    基于spring-cloud-gateway网关的解决方案,因为该网关使用的是reactor模式的webflux,所以:

    @Configuration
    public class CommonConfig {
    
        @Bean
        public CorsWebFilter corsWebFilter() {
            return new CorsWebFilter(new CorsConfigurationSource() {
                @Override
                public CorsConfiguration getCorsConfiguration(ServerWebExchange serverWebExchange) {
                    CorsConfiguration corsConfiguration = new CorsConfiguration();
                    corsConfiguration.setAllowCredentials(true);//是否放行cookied
                    corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);//所有的请求头都允许
                    corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);//所有的请求方法GET POST等都允许跨域
                    corsConfiguration.addAllowedOrigin(CorsConfiguration.ALL);//所有的访问来源都允许跨域
                    return corsConfiguration;
                }
            });
    
    
        }
    }

    如果是基于zuul作为网关,或者其他基于servlet的服务:

    @Configuration
    public class CorsConfig implements WebMvcConfigurer {
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600);
        }
    }
  • 相关阅读:
    jdbc在项目中的应用
    第五次作业-springmvc对表单的获取
    jquery的文档处理(部分)
    测试报告怎么写合理
    WebService的简单应用
    hdu--5078--orz
    hdu--5074--dp
    hdu--5108--数论
    hdu--5072--容斥原理
    hdu--3853--概率dp
  • 原文地址:https://www.cnblogs.com/yangxiaohui227/p/14212695.html
Copyright © 2011-2022 走看看