spring cloud 跨域CORS 报错 403

在Spring Cloud项目中遇到CORS跨域403错误,最初尝试通过Zuul过滤器配置解决,但发现请求无法正常通过Ribbon层。在Ribbon主类中添加相应配置,并且需要注释掉Zuul的相关配置以消除冲突。

spring cloud 跨域CORS 报错 403

针对这个问题,之前是在zuul中过滤器配置跨域:

	HttpServletRequest request = ctx.getRequest();
	HttpServletResponse response = ctx.getResponse();
	
	response.setHeader("Access-Control-Allow-Headers", "Authentication");
	response.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE");
	response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

    if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
        response.setStatus(HttpServletResponse.SC_OK);
    }

但是这样配置后,查看日志,接口有请求到网关,但是走不下去了,检查到ribbon层的时候;

在ribbon的主类也加上配置:
这边导入的包:

import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Bean
 public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        // 允许cookies跨域
        config.setAllowCredentials(true);
        // #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
        config.addAllowedOrigin("*");
        // #允许访问的头信息,*表示全部
        config.addAllowedHeader("*");
        // 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
        config.setMaxAge(5000);
        // 允许提交请求的方法,*表示全部允许
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

ribbon这边配置后,又需要将zuul的

// response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

注释掉,否则会冲突(不解)

### Spring Cloud 配置方法 在 Spring Cloud 中,问题(CORS, Cross-Origin Resource Sharing)可以通过多种方式进行配置。一种常见的方式是在网关层面上通过 `CorsGlobalFilter` 配置全局的支持[^1]。 #### 使用 CorsGlobalFilter 进行全局配置 下面展示了一个利用 `CorsGlobalFilter` 实现访问控制的例子: ```java import org.springframework.context.annotation.Bean; import org.springframework.web.cors.CorsConfiguration; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.cloud.gateway.filter.CorsGlobalProperties; @Bean public CorsGlobalProperties corsGlobalProperties() { CorsGlobalProperties properties = new CorsGlobalProperties(); CorsConfiguration configuration = new CorsConfiguration(); configuration.addAllowedHeader("*"); configuration.addAllowedMethod("*"); configuration.setAllowCredentials(true); configuration.addAllowedOriginPattern("*"); properties.setAddToSimpleRequests(false); // 可选,默认false properties.setCorsConfigurations(Collections.singletonMap("/**", configuration)); return properties; } ``` 此段代码创建了允许任何源、头部和HTTP 方法发起请求并携带凭证的 CORS 设置,并将其应用到所有的路径上。 另一种方式则是直接定义一个 `WebMvcConfigurer` Bean 来添加映射规则,这种方式适用于更细粒度地管理哪些 URL 模式应该启用 CORS 政策[^4]: ```java @Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry){ registry.addMapping("/**") .allowedOriginPatterns("*") .allowedHeaders("*") .allowedMethods("*") .allowCredentials(true) .maxAge(TimeUnit.DAYS.toSeconds(1)); } }; } ``` 上述两种方案都可以有效地解决 Spring Cloud 应用中的资源共享问题。具体采用哪种取决于项目需求和个人偏好[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值