BaseX REST API:设置自定义HTTP响应标头 [英] BaseX REST API: Set custom HTTP response header
问题描述
我想在 BaseX REST API 的所有响应中包含以下HTTP标头:
I want to include the following HTTP header to all responses by the BaseX REST API:
Access-Control-Allow-Origin: *
这可能吗?
推荐答案
BaseX在引擎盖下使用Jetty.您可以修改web.xml
文件,以使Jetty发送CORS标头,但是
BaseX uses Jetty below the hood. You can modify the web.xml
file to make Jetty send CORS headers, but either
- 至少使用添加了
jetty-servlets
库的BaseX 8.6.3或 - 必须添加
jetty-servlets
jar到您的$CLASSPATH
(BaseX已经发布了jetty-servlet
,这是一个不同的类;请确保获取与BaseX中包含的内容相匹配的适当版本).
- use at least BaseX 8.6.3 which added the
jetty-servlets
library or - have to add the
jetty-servlets
jar to your$CLASSPATH
(BaseX already shipsjetty-servlet
, which is a different class; and be sure to fetch the appropriate version matching what's included in BaseX).
在web.xml
文件中包括以下指令:
Include following directives to the 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: *
:,而默认值已经
Be aware that Jetty does not seem to support posting a wildcard header Access-Control-Allow-Origin: *
: while the default is already
<init-param>
<param-name>allowedOrigins</param-name>
<param-value>*</param-value>
</init-param>
(将其放入<filter/>
元素中),Jetty使用它来构造一个正则表达式,并且如果匹配则始终返回Origin:
请求标头的值,但这也可以为您带来好处.
(put that into the <filter/>
element), Jetty uses this to construct a regular expression and always returns the value of the Origin:
request header if matching, but that should also serve you well.
一个示例请求:
$ 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
鉴于此请求似乎很合理,您可能会成功打开一个问题默认情况下包括该库,甚至可能默认情况下启用CORS.(该库现在默认包含在库中)
Given this seems a rather reasonable request and thing to do, you might be successful opening an issue to include the library by default, and maybe even enabling CORS by default. (the library is now included by default)
这篇关于BaseX REST API:设置自定义HTTP响应标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!