为了账号安全,请及时绑定邮箱和手机立即绑定

如何解决Spring Boot应用程序中的CORS问题?

如何解决Spring Boot应用程序中的CORS问题?

慕雪6442864 2024-01-25 21:29:26
我尝试使用 js 代码来获取远程(spring boot)js 代码,它显示Cross-Origin Read Blocking (CORB) blocked cross-origin response http://45.82.79.208/ with MIME type text/plain. See https://www.chromestatus.com/feature/5629709824032768 for more details我的js代码:var script = document.createElement("script");script.type = "text/javascript";script.src = "http://45.82.79.208/";document.head.appendChild(script);我的 Spring Boot 代码,允许 cros:@Configurationpublic class CorsConfig implements WebMvcConfigurer {    @Override    public void addCorsMappings(CorsRegistry registry) {        registry.addMapping("/**")  // allow cros path        .allowedOrigins("*")    // allow cros origin        .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")// allow method        .maxAge(168000) //        .allowedHeaders("*")        .allowCredentials(true);    }}我的代码有问题吗?如何解决此问题以允许 CORS?
查看完整描述

2 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

根据该页面,响应类型不正确。可以尝试设置js的响应头(content-type)

它将阻止从 a 或 标记请求的跨源文本/html 响应,并将其替换为空响应


查看完整回答
反对 回复 2024-01-25
?
蓝山帝景

TA贡献1843条经验 获得超7个赞

启用 CORS 非常简单 - 只需将注释 @CrossOrigin 添加到您的控制器即可。

查看完整回答
反对 回复 2024-01-25
  • 2 回答
  • 0 关注
  • 33 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信