在前后端分离项目中,经常会遇到跨域问题,
跨源资源共享(CORS)
(一)使用nginx部署为同一域
![在这里插入图片描述](https://img-blog.csdnimg.cn/c77ff37d51a54551881324779a65b3d2.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);
}
}