Symfony 5 Mercure 无法发送“https://127.0.0.1:8000/.well-known/mercure”的更新 SSL 连接错误

问题描述

我是学生和初学者,如果我不明白,请提前道歉。 我尝试将 Symfony 5 与 mercure 一起使用,但我很难将更新发送到集线器。 我将文档检查到 Symfony Mercure doc 并认为应该对此做出很好的贡献,但不是全部。

我已经在我的 Windows 10 上安装了 docker,我直接通过 Symfony cli 安装了 Mercure,就像在文档上写的一样。 我的参数是cli认安装的参数。 .env

        ###> symfony/mercure-bundle ###
    # See https://symfony.com/doc/current/mercure.html#configuration
    # The URL of the Mercure hub,used by the app to publish updates (can be a local URL)
    MERCURE_URL=https://127.0.0.1:8000/.well-kNown/mercure
    # The public URL of the Mercure hub,used by the browser to connect
    MERCURE_PUBLIC_URL=https://127.0.0.1:8000/.well-kNown/mercure
    # The secret used to sign the JWTs
    MERCURE_JWT_SECRET="secret"
###< symfony/mercure-bundle ###

我使用真正的 JWT 秘密只是为了例如([JWT.io])2

{
  "mercure": {
    "publish": [
      "*"
    ]
  }
}

用我的秘密密码。

我使用 eventSource 创建了一个 js 文件

const eventSource = new EventSource('http://127.0.0.1:8000/.well-kNown/mercure?topic=' + encodeURIComponent('http://127.0.0.1:8000/commande/recapitulatif'),);
eventSource.onmessage = event => {
    // Will be called every time an update is published by the server
    alert("Commande");
    console.log(JSON.parse(event.data));
}

并且我希望在有人访问回顾订单时发送通知http://127.0.0.1:8000/commande/recapitulatif。 在我的控制器中,当地址 http://127.0.0.1:8000/commande/recapitulatif调用时,我添加

 $update = new Update(
            'http://127.0.0.1:8000/commande/recapitulatif',json_encode(['status' => 'Commande'])
        );

        $hub->publish($update);

在我需要通知添加js代码页面上,如果我在chrome的应用程序开发者的网络中检查与mercure的连接,我可以看到:

Request URL: http://127.0.0.1:8000/.well-kNown/mercure?topic=http%3A%2F%2F127.0.0.1%3A8000%2Fcommande%2Frecapitulatif
Request Method: GET
Status Code: 200 OK
Remote Address: 127.0.0.1:8000
Referrer Policy: strict-origin-when-cross-origin

但是当我尝试转到 url http://127.0.0.1:8000/commande/recapitulatif 时出现错误(无法发送更新)和(“https://127.0.0.1:8000/.well-kNown/”的 SSL 连接错误水星”。)

更新: 我改变了

MERCURE_URL=https://127.0.0.1:8000/.well-kNown/mercure

MERCURE_PUBLIC_URL=https://127.0.0.1:8000/.well-kNown/mercure

MERCURE_URL=http://127.0.0.1:8000/.well-kNown/mercure

MERCURE_PUBLIC_URL=http://127.0.0.1:8000/.well-kNown/mercure

现在出现错误(无法发送更新)我有(HTTP/1.1 401 Unauthorized 返回“http://127.0.0.1:8000/.well-kNown/mercure”。)

可能我需要将我的应用程序放到 https 以便与 mercure 建立 ssl 连接?

再试一次。 我替换了 hhttp,我将 https 更改为 http 到我的 .env 和 js 文件。 之后,我像这样更改控制器中的代码

 $discovery->addLink($request);

    $response = new JsonResponse([
        '@id' => 'http://127.0.0.1:8000/commande/recapitulatif','status' => 'Order',]);

    $response->headers->setCookie(
        $authorization->createCookie($request,['http://127.0.0.1:8000/commande/recapitulatif'])
    );

现在我没有错误,但没有通知我的浏览器。

我想要通知页面的日志:

[Application] Jun 26 17:33:45 |DEBUG  | SECURI Stored the security token in the session. key="_security_main"
[Web Server ] Jun 26 19:33:45 |INFO   | SERVER GET  (200) /favicon.ico ip="127.0.0.1"
[Application] Jun 26 17:33:46 |INFO   | REQUES Matched route "_wdt". method="GET" request_uri="http://127.0.0.1:8000/_wdt/8f2711" route="_wdt" route_parameters={"_controller":"web_profiler.controller.profiler::toolbaraction","_route
":"_wdt","token":"8f2711"}
[Web Server ] Jun 26 19:33:47 |INFO   | SERVER GET  (200) /_wdt/8f2711 ip="127.0.0.1"

以及我需要发送通知的日志:

[Application] Jun 26 17:36:06 |DEBUG  | SECURI Stored the security token in the session. key="_security_main"
[Web Server ] Jun 26 19:36:06 |INFO   | SERVER POST (200) /commande/recapitulatif host="127.0.0.1:8004" ip="127.0.0.1" scheme="https"
[Application] Jun 26 17:36:07 |INFO   | REQUES Matched route "_wdt". method="GET" request_uri="http://127.0.0.1:8000/_wdt/d13a0f" route="_wdt" route_parameters={"_controller":"web_profiler.controller.profiler::toolbaraction","token":"d13a0f"}
[Web Server ] Jun 26 19:36:08 |INFO   | SERVER GET  (200) /_wdt/d13a0f ip="127.0.0.1"

但是如果我像这样添加更新:

 $update = new Update(
                'http://127.0.0.1:8000/commande/recapitulatif',json_encode(['status' => 'Commande']),true
            );

            $hub->publish($update);

我的日志是这样的:

[Application] Jun 26 18:02:39 |INFO   | HTTP_C Request: "POST http://127.0.0.1:8000/.well-kNown/mercure"
[Application] Jun 26 18:02:39 |INFO   | HTTP_C Response: "401 http://127.0.0.1:8000/.well-kNown/mercure"
[Application] Jun 26 18:02:39 |CRITICA| REQUES Uncaught PHP Exception Symfony\Component\Mercure\Exception\RuntimeException: "Failed to send an update." at C:\laragon\www\Projet\Restaurant\RelaisDesVoutes\vendor\symfony\mercure\src\H
ub.PHP line 104
[Web Server ] Jun 26 20:02:39 |INFO   | MERCUR Topic selectors not matched,not provided or authorization error
[Web Server ] Jun 26 20:02:39 |WARN   | SERVER POST (401) /.well-kNown/mercure host="127.0.0.1:8004" ip="127.0.0.1" scheme="https"
[Application] Jun 26 18:02:39 |DEBUG  | SECURI Stored the security token in the session. key="_security_main"
[Web Server ] Jun 26 20:02:39 |ERROR  | SERVER POST (500) /commande/recapitulatif host="127.0.0.1:8004" ip="127.0.0.1" scheme="https"

如果有人可以帮助我了解我的问题在哪里,非常感谢。

解决方法

我假设您已经运行并安装了单独的 Mercure 二进制文件。您不能同时在端口 8000 上同时运行 Mercure 二进制文件和 Symfony。根据您的版本(由于我遵循的教程,我正在运行 Mercure v0.9.0)您可能可以制作这样的 Makefile

mercure_server:
    JWT_KEY=my_key ADDR=localhost:3000 ALLOW_ANONYMOUS=1 CORS_ALLOWED_ORIGINS=http://localhost:8000 ./bin/mercure

然后运行 ​​make mercure_server

这假设您在项目的 bin 目录中有 mercure 二进制文件。此命令不适用于最新版本的 Mercure,因为它不是如何设置环境变量的。

较新版本的 Mercure 二进制文件运行的是 Caddy Web 服务器的修改版本,并且与 Symfony 的兼容性从我目前所读到的内容来看并没有很好的记录。如果您有困难,我会考虑降级到 v0.9.0,因为有更多可用资源。