Skip to content

springBoot项目默认不允许跨域

问题

前后端项目都在本地启动,页面访问接口的时候,报跨域错误

解决办法

访问本地项目

项目:misscmszb/WebConfiguration

在webMvc配置文件里重写addCorsMappings方法,配置允许跨域

java
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
}

访问服务器部署的项目

使用nginx进行允许跨域配置

如有转载或 CV 的请标注本站原文地址