【发布时间】:2016-11-23 17:59:07
【问题描述】:
我正在开发一个网页,基本上通过8080端口连接到Flume服务器。每次客户端发送新的请求时,它都会抛出403 Forbidden错误,因为HTTP源不知道如何处理OPTIONS请求。
Apache Flume 的文档中没有具体提到如何启用 CORS。
【问题讨论】:
我正在开发一个网页,基本上通过8080端口连接到Flume服务器。每次客户端发送新的请求时,它都会抛出403 Forbidden错误,因为HTTP源不知道如何处理OPTIONS请求。
Apache Flume 的文档中没有具体提到如何启用 CORS。
【问题讨论】:
原来我不得不修改 Flume 的源代码。
在文件HTTPSource(私有类FlumeHTTPServlet)中,添加如下方法:
@Override
public void doOptions(HttpServletRequest request, HttpServletResponse response)
throws IOException
{
response.addHeader("Access-Control-Allow-Origin", "*");
}
【讨论】:
我在 flume 前使用 nginx 代理来设置 CORS 标头,而不是使用 flume。
这是我的代理定义:
server {
#proxy server for flume
listen 15140; ## listen for ipv4; this line is default and implied
location / {
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin $http_origin always;
add_header Access-Control-Allow-Credentials true always;
add_header Access-Control-Allow-Methods 'GET,POST,PUT,DELETE,OPTIONS' always;
add_header Access-Control-Allow-Headers 'Authorization,X-Requested-With,Content-Type,Origin,Accept,token' always;
add_header Access-Control-Max-Age 3600;
add_header Content-Length 0;
return 200;
}
add_header Access-Control-Allow-Origin $http_origin always;
add_header Access-Control-Allow-Credentials true always;
add_header Access-Control-Allow-Methods 'GET,POST,PUT,DELETE,OPTIONS' always;
add_header Access-Control-Allow-Headers 'Authorization,X-Requested-With,Content-Type,Origin,Accept,token' always;
proxy_pass http://localhost:5140/;
}
希望这会有所帮助
【讨论】: