问题描述
我已经阻止这个问题 2 天了。我正在尝试为 Mercure 设置身份验证,以便客户可以订阅“私人”集线器。所以我按照 Symfony 和 Mercury 文档中的规定配置了我的环境变量,它们是:
我的应用程序 Symfony 的 .env:
###> mercure/bundle ###
MERCURE_URL=http://mydemoapp.com:80/.well-kNown/mercure
MERCURE_PUBLIC_URL=http://mydemoapp.com:80/.well-kNown/mercure
MERCURE_JWT_SECRET=MySecretKeyJWT
MERCURE_JWT_TOKEN=MyTokenJWT
###< mercure/bundle ###
我的球童文件:
{
# Debug mode (disable it in production!)
debug
# HTTP/3 support
experimental_http3
}
:80
log
route {
redir / /.well-kNown/mercure/ui/
encode gzip
mercure {
# Enable the demo endpoint (disable it in production!)
demo
# Publisher JWT key
publisher_jwt MySecretKeyJWT
# Subscriber JWT key
subscriber_jwt MySecretKeyJWT
# CORS
cors_origins http://127.0.0.1:3005
# Allow anonymous subscribers (double-check that it's what you want)
anonymous
# Enable the subscription API (double-check that it's what you want)
subscriptions
}
respond "Not Found" 404
}
我的容器美居:
mercure:
image: dunglas/mercure
container_name: mercure
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- mercure:/data
labels:
- traefik.docker.network=proxy
- traefik.enable=true
- traefik.http.routers.mercure.rule=Host(`mydemoapp.com`)
expose:
- "80"
networks:
- app
这里是我的控制器的代码,用于生成授权 cookie 和发布集线器的路由:
我的路线授权:
/**
* @Route("/api/v1.0/ms-security/authorization",name="security.index",methods={"GET"})
*/
public function index(Authorization $authorization,Request $request): Response
{
$response = $this->json([
'message' => 'Your authorization has been generated !','code' => '200',]);
$response->headers->setCookie($authorization->createCookie($request,["http://mydemoapp.com:80/api/v1.0/ms-security/23"]));
return $response;
}
我在集线器上发布的路由(此路由只是一个测试路由,用于了解我的客户端是否订阅良好并收到通知):
/**
* @Route("/api/v1.0/ms-match/invitations/test",name="invitation.test",methods={"GET"})
*/
public function test(HubInterface $hub)
{
$update = new Update("{$this->getParameter('base.url')}/ms-security/23",json_encode("Hy it's me {$this->getUser()->getId()} !"),true);
$hub->publish($update);
return $this->json(["message" => "ok"]);
}
和我的 javascript :
async mounted(){
let data = {"phone": "myPhone","password": "myPassword"};
const resAuth = await fetch("http://mydemoapp.com:80/api/v1.0/ms-security/login",{method: "POST",headers: { 'Content-Type': 'application/json' },body: JSON.stringify(data)});
const dataAuth = await resAuth.json();
console.log(dataAuth);
const res = await fetch("http://mydemoapp.com:80/api/v1.0/ms-security/authorization",{method: "GET",headers: {'Authorization': `Bearer ${await dataAuth.token}`}});
const dataCookie = await res.json();
console.log(dataCookie)
const url = new URL('http://mydemoapp.com:80/.well-kNown/mercure');
url.searchParams.append('topic','http://mydemoapp.com:80/api/v1.0/ms-security/23');
const eventSource = new EventSource(url,{withCredentials: true});
console.log(eventSource.withCredentials)
eventSource.onmessage = e => console.log(e.data);
}
当我调用我的路由“授权”时,我在响应标头中看到我确实有要发送的 cookie。当我在 JWT.IO 上解码它时,我们可以看到我的 jwt 包含订阅这个集线器的信息,但是当我调用我的测试路由时,如果我私下发送这个集线器,客户端不会收到通知(在公共场合一切都很顺利好)。所以我的印象是cookies不发送。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)