【问题标题】:BaseX REST API: Set custom HTTP response headerBaseX REST API:设置自定义 HTTP 响应标头
【发布时间】:2017-08-13 10:45:22
【问题描述】:

我想在BaseX REST API 的所有响应中包含以下 HTTP 标头:

Access-Control-Allow-Origin: *

这可能吗?

【问题讨论】:

    标签: rest http cors jetty basex


    【解决方案1】:

    BaseX 在引擎盖下使用 Jetty。您可以修改 web.xml 文件以使 Jetty 发送 CORS 标头,但要么

    • 至少使用添加了jetty-servlets 库的BaseX 8.6.3 或
    • 必须将jetty-servlets jar 添加到您的$CLASSPATH(BaseX 已经提供jetty-servlet,这是一个不同的类;并确保获取与 BaseX 中包含的内容相匹配的适当版本)。

    在web.xml 文件中包含以下指令:

    <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>
    

    (将其放入 &lt;filter/&gt; 元素中),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
    

    鉴于这似乎是一个相当合理的请求和要做的事情,你可能会成功opening an issue 默认包含该库,甚至可能默认启用 CORS。(现在包含该库默认)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 2012-02-28
    • 2020-02-09
    • 1970-01-01
    • 2018-11-08
    • 2015-01-24
    相关资源
    最近更新 更多