Appearance
了解跨域相关问题:
[跨域](section-id={D9A41BF5-4D5A-4262-A0C7-C61D3661B1C9})
在ly-gateway中新建包com.leyou.gateway.config.GlobalCorsConfig.java
packagecom.leyou.gateway.config;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.cors.CorsConfiguration;
importorg.springframework.web.cors.UrlBasedCorsConfigurationSource;
importorg.springframework.web.filter.CorsFilter;
@Configuration
publicclassGlobalCorsConfig{
@Bean
public CorsFilter corsFilter(){
//添加CORS配置信息
UrlBasedCorsConfigurationSource configSource=new UrlBasedCorsConfigurationSource();
CorsConfiguration config=new CorsConfiguration();
//是否发送Cookie信息
config.setAllowCredentials(true);
//允许的域,不要写*,否则cookie就无法使用了
config.addAllowedOrigin("*");
//允许的头信息
config.addAllowedHeader("*");
//允许的请求方式
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
//5)有效时长
config.setMaxAge(3600L);
//2.添加映射路径,我们拦截一切请求
configSource.registerCorsConfiguration("/**",config);
//3.返回新的CorsFilter
return new CorsFilter(configSource);
}
}