解决前后端跨域报错:has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘
·
一、报错信息
has been blocked by CORS policy: No 'Access-Control-Allow-Origin'
header is present on the requested resource.
来自一个IP端口的页面(前端项目),要访问另一个IP端口的资源(springboot请求接口),会产生跨域访问。
二、解决方法:增加配置类CorsConfig
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry){
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedHeaders(CorsConfiguration.ALL)
.allowedMethods(CorsConfiguration.ALL)
.allowCredentials(true)
.maxAge(3600);
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)