springcloudzuul解决跨域访问

编程

spring zuul是不支持跨域的,需要对其进行配置

1.配置 application.yml

zuul:

sensitive-headers: Access-Control-Allow-Origin

ignored-headers: Access-Control-Allow-Origin,Vary,X-Frame-Options,token

猜测关键点在第二行,忽略Access-Control-Allow-Origin这种方式;

2.配置filter

@Configuration

public class GatewayCrosConfig {

@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);

}

}

3. zuul 与cloud内部服务的跨域问题

zuul内部转发是不存在跨域问题的

4. spring mvc 配置跨域

@Configuration

public class WebConfig extends WebMvcConfigurationSupport {

@Override

public void addCorsMappings(CorsRegistry registry) {

registry.addMapping("/**")

.allowedHeaders("Content-Type","X-Requested-With","accept,Origin","Access-Control-Request-Method","Access-Control-Request-Headers","token")

.allowedMethods("*")

.allowedOrigins("*")

.allowCredentials(true);

}

}

通过设置cors配置来实现

以上是 springcloudzuul解决跨域访问 的全部内容, 来源链接: utcz.com/z/510426.html

回到顶部