首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony / Mercure / JS EventSource不发送cookie

Symfony / Mercure / JS EventSource不发送cookie
EN

Stack Overflow用户
提问于 2021-06-18 17:38:22
回答 1查看 229关注 0票数 2

我已经阻止这个问题2天了。我正在尝试为Mercure设置身份验证,以便客户可以订阅“私有”集线器。所以我按照Symfony和Mercury文档中的规定配置了我的环境变量,如下所示:

我的应用Symfony的.env:

代码语言:javascript
复制
###> 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 ###

我的caddyFile:

代码语言:javascript
复制
{
    # 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:

代码语言:javascript
复制
  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和发布集线器的路由的代码:

我的路由授权:

代码语言:javascript
复制
    /**
     * @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;
    }

用于在集线器上发布的我的路由(此路由只是一个测试路由,用于了解我的客户端是否订阅良好并接收通知):

代码语言:javascript
复制
  /**
     * @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:

代码语言: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);
  }

当我将我的路由称为"authorization“时,我在响应头中看到我确实有要发送的cookie。当我在JWT.IO上解码它时,我们可以看到我的jwt包含了订阅这个集线器的信息,但是当我调用我的测试路由时,如果我私下发送这个集线器,客户端就不会收到通知(在公开的情况下,一切都很顺利)。所以我的印象是cookies不会发送。

EN

回答 1

Stack Overflow用户

发布于 2021-08-19 11:13:07

我也有类似的问题,但我只能给你一些关于不理想的解决方案的提示。某些浏览器在组合使用不同端口时,在localhost下设置cookie存在问题。端口为8000 (单独的Set服务器)的Symfony设置cookie -端口为8080的UI不会在localhost下显示它,尽管报头中的响应通过"Set-Cookie“传输相应的令牌。另一方面,在生产系统中(具有相同的UI和API端口),一切都正常工作。

作为一种变通方法,我从响应头(也可以在jwt.io上生成)中获取有效的令牌。在Mercure的调试工具(http://localhost:3000)的设置部分中,我选择了cookie类型并插入了生成的令牌。如果单击"Discover“,则设置cookie,也可以在UI中访问该cookie。在设置cookie时,mercure可能使用限制较少的设置。我尝试了许多其他设置,但没有找到直接的解决方案。使用本地隧道也可能解决这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68032836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档