问题:as been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
浏览器会发送一个option预检,请问是否可以跨域
解决方法1.使用nginx反向代理把请求都指向nginx 然后转发出去
2.在网关项目里边定义一个跨域配置类
代码如下
package com.atguigu.gulimall.gateway.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
@Configuration //这是一个配置类
public class GuliCorsConfiguration {
@Bean //放入到容器中
public CorsWebFilter corsWebFilter(){
//配置跨域配置信息
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
//1.配置跨域
corsConfiguration.addAllowedHeader("*");//允许所有头
corsConfiguration.addAllowedMethod("*");//允许所有方法
corsConfiguration.addAllowedOrigin("*");//允许所有来源
corsConfiguration.setAllowCredentials(true);//允许携带cookie
source.registerCorsConfiguration("/**",corsConfiguration);
return new CorsWebFilter(source);
}
}