【问题标题】:Mercure + Angular + Symfony >>> all on local machineMercure + Angular + Symfony >>> 全部在本地机器上
【发布时间】:2019-11-06 11:35:10
【问题描述】:

我在 Mac OS 10.14.6 上使用 Angular 8、Symfony 4.3 和 Mercure 0.72 darwin。我对symfony很陌生。 我只想将更新发送给本地机器上的授权客户端。到目前为止,这确实是一个很大的痛苦。我希望我能在这里找到一些帮助或答案。

我得到的最后一个错误是

http: TLS handshake error from ip:55289: acme/autocert: unable to authorize "mydyndns-url.com"; challenge "http-01" failed with error: acme: authorization error for mydyndns-url.com: 400 urn:acme:error:connection: Fetching http://mydyndns-url.com/.well-known/acme-challenge/SomeHash: Error getting validation data; challenge "tls-alpn-01" failed with error: acme: authorization error for mydyndns-url.com: 400 urn:acme:error:connection: Connection refused

加

http: TLS handshake error from ip:56803: acme/autocert: missing certificate

我没有可以测试任何东西的远程服务器。 但我想向授权客户发送一些更新。我没有办法在本地测试吗?没有任何 sll tls 的东西? 到目前为止,如果我理解正确,要在客户端(cookie 或承载)中使用授权,我必须加密mercure 通信,为此我需要一个证书,该证书是我用letsencrypt 为我的dyndns url 创建的,我也是为此创建的,但是然后我还读到加密授权必须在 443 上运行,但都在一台机器上,所以我现在需要为所有本地测试服务器创建子域吗? (因为我的 dyndns 在使用 sudo certbot certonly --standalone 创建证书时出现问题,它给我的子域挑战失败,然后是 dyndns 提供者的子子域),那么他为什么还要尝试获取 :80 服务器.well-known 路径的授权?

但是因为我觉得自己像掉进了 alices rabbithole 并且没有尽头,所以我不知道用 mercure 尝试任何东西是否还有意义。

我只想在“/messages”上推送一条消息以定位“/user/patata”,然后在我的 Angular 客户端中使用 cookie 获取该消息。在本地开发服务器上做到这一点有那么难吗?该用例是否有完整的文档?

使用它来启动mercure

sudo PUBLISHER_JWT_KEY='mercure_key' SUBSCRIBER_JWT_KEY='mercure_key' JWT_KEY='mercure_key' ACME_HOSTS='mydyndns-url.com' ADDR='mydyndns-url.com:443' CERT_FILE='/Users/me/Projects/fullchain.pem' KEY_FILE='/Users/me/Projects/privkey.pem' CORS_ALLOWED_ORIGINS='*' ALLOW_ANONYMOUS='1' debug='1' ./mercure

当然还有 symfony 设置和代码以及角码等等。但把所有东西都放在这里就太过分了。大多数情况下,我想知道如何简化这一点,而不是让你头晕目眩:D

【问题讨论】:

    标签: mercure


    【解决方案1】:

    好的,我设法解决了它(还不完美,但正在运行,无论如何它只是用于开发)。

    • 当然我生成了我的 JWT https://jwt.io/#debugger-io
    • 按照 symfony https://symfony.com/doc/current/mercure.html#running-a-mercure-hub 的 mercure 设置
    • 在 Symfony 中安装 composer require symfony/mercure-bundle
    • 我从https://www.dynu.com/ 得到了一个带有通配符的 dyndns
    • 我将路由器 (fritzbox) 配置为 forward port 80 and 443 http。 (暂时,仅用于创建 certbot 证书!!!)
    • 我打电话给sudo ifconfig lo0 alias [public IP] up
    • 我将我的 dyndns 域的三个子域添加到我的 /etc/hosts 文件 (client.mydomain.dynu.org,api.mydomain.dynu.org,hub.mydomain.dynu.org) 绑定到我的公共 IP。
    • 我使用letsencrypt certbot 和sudo certbot certonly --standalone 并为4 个域(client.mydomain.dynu.org,api.mydomain.dynu.org,hub.mydomain.dynu.org,mydomain.dynu.org) 创建了一个证书
    • 然后我更改了我的 /etc/hosts 文件并将所有 4 个域绑定到 127.0.0.1
    • 我将我的 apache 配置为加载代理插件
    LoadModule proxy_module lib/httpd/modules/mod_proxy.so
    LoadModule proxy_http_module lib/httpd/modules/mod_proxy_http.so
    
    • 我为我的 apache 设置了一个虚拟主机,用于我的 symfony api. 和 angular client. 子域(我不使用 symfony server:start)
    <VirtualHost api.mydomain.dynu.org:443>
        DocumentRoot "/Users/me/Sites/api/public"
        ServerName api.mydomain.dynu.org
        SSLEngine on
        SSLCertificateFile "/Users/me/Projects/mydomain.dynu.org.crt"
        SSLCertificateKeyFile "/Users/me/Projects/mydomain.dynu.org.key"
        ErrorLog "/Users/me/Sites/api/error_log"
        CustomLog "/Users/me/Sites/api/access_log" common
    </VirtualHost>
    
    <VirtualHost client.mydomain.dynu.org:443>
        ServerName client.mydomain.dynu.org
        SSLEngine On
        SSLProxyEngine On
        ProxyRequests Off
        SSLCertificateFile "/Users/me/Projects/mydomain.dynu.org.crt"
        SSLCertificateKeyFile "/Users/me/Projects/mydomain.dynu.org.key"
    
        ProxyPass / http://client.mydomain.dynu.org:444/
        ProxyPassReverse / http://client.mydomain.dynu.org:444/
    </VirtualHost>
    
    <VirtualHost hub.mydomain.dynu.org:443>
        ServerName hub.mydomain.dynu.org
        SSLEngine On
        SSLProxyEngine On
        ProxyRequests Off
        SSLCertificateFile "/Users/me/Projects/mydomain.dynu.crt"
        SSLCertificateKeyFile "/Users/me/Projects/mydomain.dynu.key"
    
        ProxyPass / http://hub.mydomain.dynu.org:445/
        ProxyPassReverse / http://hub.mydomain.dynu.org:445/
    </VirtualHost>
    
    • 我在 symfony 中设置:
    in '.env':
    MERCURE_PUBLISH_URL=https://hub.mydomain.dynu.org:443/hub
    CORS_ALLOW_ORIGIN=^https://client.mydomain.dynu.org$
    
    in 'nelmio_cors.yaml':
    nelmio_cors:
        defaults:
            ...
            origin_regex: true
        paths:
            ...
            '^/api/':
                allow_origin: ['^https://client.mydomain.dynu.org']
                allow_headers: ['Content-Type', 'Authorization']
                allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'OPTIONS']
                max_age: 3600
            ...
    
      1234563到目前为止,知道如何更好地处理这些非 .ts 文件。 (也许有人可以提供一个真正的解决方案)
    • 我在 symfony 中生成的令牌

            $username = $this->getUser()->getUsername(); // Retrieve the username of the current user
            $token = (new Builder())
                // set other appropriate JWT claims, such as an expiration date
                ->withClaim('mercure', ['subscribe' => ["/user/patata"], 'publish' => ['*']]) // could also include the security roles, or anything else
                ->sign(new Sha256(), 'mercure_key') // don't forget to set this parameter! Test value: aVerySecretKey
                ->getToken();
    
            $response = $this->json(['token' => sprintf('%s', $token)]);
    
    • 我在端口 444 sudo ng serve --host client.mydomain.dynu.org --port 444 上运行 angular,apache 反向代理将 443 上的所有请求转发到该端口

    • 我使用

    • 启动美居
    sudo PUBLISHER_JWT_KEY='mercure_key' SUBSCRIBER_JWT_KEY='mercure_key' \
    JWT_KEY='mercure_key' ADDR='hub.mydomain.dynu.org:445' \
    CORS_ALLOWED_ORIGINS='*' ALLOW_ANONYMOUS='1' DEBUG='1' ./mercure
    

    请小心遵循上述所有内容: - 我对 symfony 很陌生,以前总是使用 flowframework。 - 我以前从来不需要处理证书的设置或配置 - 而且我从来不用处理任何加密设置,没有 JWT,没有 apache 反向代理。

    历史:
    我之前尝试过mercure cookie令牌方式,但它不起作用,并且在某处写了你需要为此启用加密,所以这对我来说意味着一种证书方式,但自我生成并没有帮助,相反我读到了关于使用letencrypt,所以我就这样走了,直到我读到的某个地方,letsencrypt 只在 443 上运行,所以我读到了代理,然后就这样走了,不确定它是否真的必须那样。
    现在我会在没有证书和东西的情况下有新的想法,我会在开始时尝试,但现在一切正常,我可以再次开发,这就是我想要的。通过这种方式,我学会了如何创建letsencrypt证书,关于反向代理和acme的含义:D。

    但如果您有任何建议、投诉、警告、改进,请告诉我!!

    【讨论】:

      猜你喜欢
      • 2021-05-09
      • 2021-12-27
      • 2018-06-17
      • 2021-09-03
      • 2011-12-05
      • 2017-01-18
      • 2017-04-27
      • 2013-08-29
      • 1970-01-01
      相关资源
      最近更新 更多