【问题标题】:mercure docker cannot configure properly problem with CORS美居码头无法正确配置 CORS 问题
【发布时间】:2020-12-18 17:47:52
【问题描述】:

我尝试了几个小时来使用 docker 配置我的 Mercure 我没有设法解决 corss 问题:

已被 CORS 策略阻止:无“访问控制允许来源” 请求的资源上存在标头

我有这个 docker-compose 文件

提取

  mercure:
    image : dunglas/mercure
    volumes:
      - ./mercure/caddy:/etc/caddy
    ports:
      - "8081:80"
      - "443:443"
    environment:
      - SERVER_NAME=localhost
      - MERCURE_SUBSCRIBER_JWT_KEY=changeIt
      - MERCURE_PUBLISHER_JWT_KEY=changeIt
      - MERCURE_CORS_ALLOWED_ORIGINS=*
      - ALLOW_ANONYMOUS=1

还有这个 Caddyfile :

# The address of your server
localhost

:80
route {
    mercure {
        # Publisher JWT key
        publisher_jwt changeIt
        # Subscriber JWT key
        subscriber_jwt changeIt
        cors_origins http://localhost:3000
    }

    respond "Not Found" 404
}

我在客户端的请求在 React 中

http.get('/api/mercure').then( response => {
            this.setCookie( response.data.token);
            const url = new URL('http://localhost:8081/.well-known/mercure');
            url.searchParams.append('topic', 'http://agora.org/votes/{id}');
            const eventSource = new EventSource(url, {withCredentials: true});
            eventSource.onmessage = (data) => {
                console.log(data);
            }
        },error => {
            console.log( error);
        })

有人可以帮我解决这个问题吗? 提前谢谢

【问题讨论】:

  • 您是否尝试过从 docker compose 文件中的环境变量中删除 MERCURE_ 前缀?我在mercure.rocks/docs/hub/config 没有看到任何东西表明这是必要的
  • 是的,我试过了,没有效果。我认为问题出在其他地方。也许服务器在发送标头之前崩溃。 https有问题。因为它重定向到 443

标签: reactjs docker mercure


【解决方案1】:

我已成功使用 docker-compose 的 Mercure,而没有出现 CORS 问题。你可以找到实现here

正如我已经到达这里,其他人也可能到达这里并且也可以帮助那个人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 2015-06-20
    • 2019-05-09
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 2017-03-23
    相关资源
    最近更新 更多