在zuul中通过过滤器配置跨域后,其它请求没问题,但是post请求403。
spring cloud 跨域问题 在 zuul 使用 ZuulFilter 解决
还有一种方式通过@Configuration配置后可以正常处理post跨域请求
import org.springframework.boot.context.embedded.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; @Configuration public class MyConfiguration { @Bean public FilterRegistrationBean corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(true); //config.addAllowedOrigin("http://domain1.com"); config.addAllowedOrigin("*"); config.addAllowedHeader("*"); config.addAllowedMethod("*"); source.registerCorsConfiguration("/**", config); FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); bean.setOrder(0); return bean; } }
参考文章:
https://spring.io/blog/2015/06/08/cors-support-in-spring-framework#filter-based-cors-support