Cors 配置与骆驼

Zei*_*oun 1 rest apache-camel cors

我有一个对 Rest Web 服务的 AJAX 请求,其中包含自定义标头“登录”。

\n\n

这是我的其余配置:

\n\n
restConfiguration()\n.component("netty4-http")\n.bindingMode(RestBindingMode.json)\n.dataFormatProperty("prettyPrint", "true")\n.enableCORS(true)\n.corsAllowCredentials(true)\n.corsHeaderProperty("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, login")\n.contextPath(contextPath).host(host).port(port);\n
Run Code Online (Sandbox Code Playgroud)\n\n

我收到对 OPTIONS 预检请求的 200 响应,但“登录”标头不是 Access-Control-Allow-Headers,并且我的浏览器从不发送实际请求。

\n\n

另外,我还没有在我的路径中为 cors 进行任何配置。

\n\n

这是我的请求标头

\n\n
\n

用户代理:Mozilla/5.0(Windows NT 10.0;Win64;x64;rv:58.0)\n Gecko/20100101 Firefox/58.0

\n\n

接受:\n text/html,application/xhtml+xml,application/xml;q=0.9, / ;q=0.8

\n\n

接受语言:fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3

\n\n

接受编码:gzip、deflate

\n\n

访问控制请求方法:GET

\n\n

访问控制请求标头:登录

\n\n

来源: http: //127.0.0.1:8081

\n\n

不饱和脂肪酸:1

\n\n

连接:保持活动状态

\n
\n\n

和响应标头:

\n\n
\n

内容长度:0

\n\n

接受:\n text/html,application/xhtml+xml,application/xml;q=0.9, / ;q=0.8

\n\n

接受编码:gzip、deflate

\n\n

接受语言:fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3

\n\n

访问控制允许凭据: true

\n\n

访问控制允许标头:来源、接受、X-Requested-With、\n 内容类型、访问控制请求方法、\n 访问控制请求标头

\n\n

访问控制允许方法:GET、HEAD、POST、PUT、DELETE、TRACE、\n OPTIONS、CONNECT、PATCH

\n\n

访问控制允许来源: http: //127.0.0.1:8081

\n\n

访问控制最大年龄:3600

\n\n

访问控制请求标头:登录

\n\n

访问控制请求方法:GET

\n\n

breadcrumbId:ID-resitt-ws-1521624297667-0-6

\n\n

不饱和脂肪酸:1

\n\n

来源: http: //127.0.0.1:8081

\n\n

用户代理:Mozilla/5.0(Windows NT 10.0;Win64;x64;rv:58.0)\n Gecko/20100101 Firefox/58.0

\n\n

连接:保持活动状态(modifi\xc3\xa9)

\n
\n\n

我觉得我的 cors 配置不会改变我的情况。

\n

Zei*_*oun 5

我们找到了答案,.enableCORS(true)必须放在.contextPath(contextPath).host(host).port(port)行后。

像这样

restConfiguration()
.component("netty4-http")
.bindingMode(RestBindingMode.json)
.dataFormatProperty("prettyPrint", "true")
.contextPath(contextPath).host(host).port(port)
.enableCORS(true)
.corsAllowCredentials(true)
.corsHeaderProperty("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, login");
Run Code Online (Sandbox Code Playgroud)