问题描述
我尝试了好几个小时来使用 docker 配置我的 mercure 我没能解决 cors 问题:
已被 CORS 政策阻止:没有“访问控制允许来源” 请求的资源上存在标头
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);
})
有人能帮我解决这个问题吗? 提前谢谢
解决方法
我已成功将 Mercure 与 docker-compose
一起使用,而没有出现 CORS 问题。您可以找到实现 here。
因为我已经到达这里,其他人也可能到达这里并且也可以帮助那个人。