在前后端分离项目中,经常会遇到跨域问题,
跨源资源共享(CORS)
(一)使用nginx部署为同一域
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8ecd752da72a332d242f2fd0e117ae80.png)
(二)配置当此请求允许跨域
@Configuration
public class GulimallCorsConfiguration {
@Bean
public CorsWebFilter corsWebFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.setAllowCredentials(true);
source.registerCorsConfiguration("/**", corsConfiguration);
return new CorsWebFilter(source);
}
}