【问题标题】:WebMvcConfigurer addCorsMappings doesn't workWebMvcConfigurer addCorsMappings 不起作用
【发布时间】:2021-11-08 08:31:39
【问题描述】:

我正在使用反向代理服务器(nginx)。它仅用于转发请求(80 到 8080)。 并且所有文件都在WAS(spring boot嵌入式tomcat)中

我想把所有进入8080端口的请求转发到80端口,反之亦然。

所以我做了一个与 CORS 相关的设置作为打击

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:80", "http://localhost:8080")
                .allowedMethods("*");
    }
}

Ajax 正在向 80 发送请求。

当我打开具有 80 个端口的网页时,它可以工作。 但是,用 8080 打开网页,它不起作用。 (CORS 策略已阻止从源“http://xx.xx.xx.xxx:8080”访问“http://xx.xx.xx.xxx/a/b”处的 XMLHttpRequest:

我必须在 nginx 中进行一些设置吗? 配置文件有问题吗?

【问题讨论】:

  • 浏览器地址栏中的地址是以http://localhost:8080开头还是以http://xx.xx.xx.xxx:8080格式的某个IP地址开头?如果地址栏显示 IP 地址而不是 http://localhost:8080,您将收到 CORS 错误。 CORS 配置的工作方式是地址栏中显示的地址的来源必须完全匹配,逐个字符,这是您在 allowedOrigins 值中配置的来源之一。
  • Chrome 也因不符合 localhost 的特殊情况规则而臭名昭著。
  • @sideshowbarker 谢谢你的评论。我刚刚把设置的内容从'localhost'改成了ip格式(.allowedOrigins("xx.xx.xx.xxx:80", "xx.xx.xx.xxx:8080")),但是还是不行。

标签: java spring-boot cors nginx-reverse-proxy


【解决方案1】:

您的反向代理服务器是否被允许使用 CORS? 80到80的客户端属于CORS,80到8080也属于。

【讨论】:

    猜你喜欢
    • 2021-03-13
    • 2019-11-25
    • 2019-12-07
    • 2023-02-05
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 2016-03-13
    相关资源
    最近更新 更多