javascript – 预检OPTIONS对SOAP服务的请求不起作用

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

我做了什么:服务器端的CORS设置(CXF)正在工作(使用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在POST请求之前发送预检OPTIONS请求.显然,SOAP不允许使用OPTIONS动词.

它不适用于SoapUI(5.0)以及CXF(2.7.7).它甚至在org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor line 130ff的注释中声明:

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

所以,我的问题是:如何修改我的SOAP服务实现(使用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>
上一篇:java-CXF Web客户端中的动态查询参数


下一篇:java – 用于字节的Spring REST模板