SpringCloud工作笔记043---SpringCloud 从整体上解决跨域问题_zuul网关配置实现跨域

  JAVA技术交流QQ群:170933152 

自己用的:

注意,测试,发现如果网关做了跨域处理,其他地方就不要再做了,比如其他微服务,否则前端

访问的时候会报跨域问题.

------------------------------------------

我的项目中是这样:

在一个非网关的服务中,我有做跨域处理:

sc-admin

   /**
     * 跨域设定
     * @param registry
     */
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        //registry.addMapping("/**");
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
//                .maxAge(3600)
//                .allowCredentials(true);
//    }

-----------------------------------------------------------

然后同时我在网关微服务中sc-gateway中也做了跨域处理:

如下面,这样的话,就导致了,通过网关访问的时候,request的header传不到sc-admin中了,

当然在sc-gateway中是可以收到的,但是在sc-admin就收不到了,这时候,把sc-admin中的跨域设置注释掉

就可以了

------------------------------------------------------------------

E:\IdeaWkSpace\SmartCommunity\sc-gateway\src\main\java\cn\gov\rongcheng\scgateway\config\GatewayConfig.java

package cn.gov.rongcheng.scgateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Component
@Configuration
public class GatewayConfig
{
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        //corsConfiguration.setAllowCredentials(true);
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }
}

-----------------------------------------------------------------------------

配置:

E:\IdeaWkSpace\SmartCommunity\sc-gateway\src\main\resources\application.properties

spring.application.name=sc-gateway
server.port=8040
zuul.host.socket-timeout-millis=60000
zuul.host.connect-timeout-millis=10000
#zuul.routes.api-a.path=/producer/**
#zuul.routes.api-a.url=spring-cloud-producer

#zuul.sensitive-headers="Cookie", "Set-Cookie", "Authorization")
zuul.sensitive-headers="Cookie","Set-Cookie"

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

#暂时先不监控接口调用速度监测
#spring.zipkin.base-url=http://localhost:9000
#spring.sleuth.sampler.percentage=1.0

----------------------------------------------------------------------------------------------------------

下面这个:是从网络上找的...

 

云环境中每个服务自己有跨域解决方案,而网关需要做最外层的跨域解决方案.如果服务已有跨域配置网关也有,会出现*多次配置问题。

head中
multiple Access-Control-Allow-Origin

使用ZUUL配置忽略头部信息

zuul:
#需要忽略的头部信息,不在传播到其他服务
  sensitive-headers: Access-Control-Allow-Origin
  ignored-headers: Access-Control-Allow-Origin,H-APP-Id,Token,APPToken

跨域配置

@Component
@Configuration
public class GateWayCorsConfig
{
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
//        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }
}

 

你可能感兴趣的:(security,Spring,Cloud)