好的,我设法解决了它(还不完美,但正在运行,无论如何它只是用于开发)。
- 当然我生成了我的 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>
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)]);
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。
但如果您有任何建议、投诉、警告、改进,请告诉我!!