【发布时间】:2020-12-18 17:47:52
【问题描述】:
我尝试了几个小时来使用 docker 配置我的 Mercure 我没有设法解决 corss 问题:
已被 CORS 策略阻止:无“访问控制允许来源” 请求的资源上存在标头
我有这个 docker-compose 文件
提取
mercure:
image : dunglas/mercure
volumes:
- ./mercure/caddy:/etc/caddy
ports:
- "8081:80"
- "443:443"
environment:
- SERVER_NAME=localhost
- MERCURE_SUBSCRIBER_JWT_KEY=changeIt
- MERCURE_PUBLISHER_JWT_KEY=changeIt
- MERCURE_CORS_ALLOWED_ORIGINS=*
- ALLOW_ANONYMOUS=1
还有这个 Caddyfile :
# The address of your server
localhost
:80
route {
mercure {
# Publisher JWT key
publisher_jwt changeIt
# Subscriber JWT key
subscriber_jwt changeIt
cors_origins http://localhost:3000
}
respond "Not Found" 404
}
我在客户端的请求在 React 中
http.get('/api/mercure').then( response => {
this.setCookie( response.data.token);
const url = new URL('http://localhost:8081/.well-known/mercure');
url.searchParams.append('topic', 'http://agora.org/votes/{id}');
const eventSource = new EventSource(url, {withCredentials: true});
eventSource.onmessage = (data) => {
console.log(data);
}
},error => {
console.log( error);
})
有人可以帮我解决这个问题吗? 提前谢谢
【问题讨论】:
-
您是否尝试过从 docker compose 文件中的环境变量中删除
MERCURE_前缀?我在mercure.rocks/docs/hub/config 没有看到任何东西表明这是必要的 -
是的,我试过了,没有效果。我认为问题出在其他地方。也许服务器在发送标头之前崩溃。 https有问题。因为它重定向到 443