我想将以下 HTTP 标头包含到所有响应中BaseX REST API http://docs.basex.org/wiki/REST:
Access-Control-Allow-Origin: *
这可能吗?
BaseX 在底层使用 Jetty。您可以修改web.xml
文件使 Jetty 发送 CORS 标头,但是
- 至少使用 BaseX 8.6.3,其中添加了
jetty-servlets
图书馆或
- 必须添加jetty-servlets https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-servlets/罐子到你的
$CLASSPATH
(BaseX 已经发货jetty-servlet
,这是一个不同的类;并确保获取与 BaseX 中包含的内容相匹配的适当版本)。
包括以下指令web.xml
file:
<web-app>
<!-- add those before the closing web-app tag: -->
<filter>
<filter-name>cross-origin</filter-name>
<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cross-origin</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
请注意,Jetty 似乎不支持发布通配符标头Access-Control-Allow-Origin: *
: 虽然默认值已经是
<init-param>
<param-name>allowedOrigins</param-name>
<param-value>*</param-value>
</init-param>
(将其放入<filter/>
元素),Jetty 使用它来构造正则表达式并始终返回该值Origin:
请求标头(如果匹配),但这也应该对您有好处。
请求示例:
$ curl -v -H "Origin: http://foo.example" http://admin:admin@localhost:8984/rest
* Trying ::1...
* Connected to localhost (::1) port 8984 (#0)
* Server auth using Basic with user 'admin'
> GET /rest HTTP/1.1
> Host: localhost:8984
> Authorization: Basic YWRtaW46YWRtaW4=
> User-Agent: curl/7.50.1
> Accept: */*
> Origin: http://foo.example
>
< HTTP/1.1 200 OK
< Content-Type: application/xml; charset=UTF-8
< Content-Length: 152
< Server: Jetty(8.1.18.v20150929)
<
<rest:databases xmlns:rest="http://basex.org/rest" resources="1">
<rest:database resources="1" size="96234589">test</rest:database>
</rest:databases>
* Connection #0 to host localhost left intact
鉴于这似乎是一个相当合理的请求和要做的事情,你可能会成功打开一个问题 https://github.com/BaseXdb/basex/issues默认包含该库,甚至可能默认启用 CORS。 (现在默认包含该库)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)