对SOAP服务的Preflight OPTIONS请求不起作用

Preflight OPTIONS request to SOAP service does not work

本文关键字:OPTIONS 请求 不起作用 Preflight SOAP 服务      更新时间:2023-09-26

我想做的:使用带有jQuery SOAP插件的jQuery从JavaScript调用跨域SOAP服务(由Remy Blom提供)。(也就是说,我在JavaScript中调用$.soap();

我所做的:服务器端(CXF)的CORS设置正在工作(使用org.eclipse.jetty.servlets.CrossOriginFilter),因此答案中存在以下内容:

Access-Control-Allow-Head...    X-Requested-With,Content-Type,Accept,Origin
Access-Control-Allow-Meth...    GET,POST,OPTIONS,HEAD
Access-Control-Allow-Orig...    http://localhost:8082
Content-Type                    application/soap+xml;charset=UTF-8

缺少什么:Firefox和Chrome在SOAP调用的POST请求之前发送预发OPTIONS请求。显然,SOAP不允许使用OPTIONS谓词。

它不适用于SoapUI(5.0)和CXF(2.7.7)。它甚至在org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor第130ff:行的一条注释中有说明

/*
 * Reject OPTIONS, and any other noise that is not allowed in SOAP.
 */

因此,我的问题是:如何修改我的SOAP servcie实现(使用CXF),以便OPTIONS请求成功返回?

即使有点晚了,我最近也遇到了同样的问题,也许这会对未来的旅行者有所帮助。

OPTIONS请求的情况下,您可能无法继续使用FilterChain。我创建了一个简单的CORSFilter,它看起来像这样:

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*
public class CORSFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        resp.addHeader("Access-Control-Allow-Origin", "*");
        resp.addHeader("Access-Control-Allow-Methods", "GET, POST");
        resp.addHeader("Access-Control-Allow-Headers", req.getHeader("Access-Control-Request-Headers"));
        if (!req.getMethod().equalsIgnoreCase("OPTIONS")) {
            chain.doFilter(request, response)
        }
    }
    @Override
    public void destroy() {}
}

我在web.xml中添加了以下内容:

<filter>
    <filter-name>CORSFilter</filter-name>
    <filter-class>CORSFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CORSFilter</filter-name>
    <url-pattern>/api/*</url-pattern>
</filter-mapping>