获取 403:在 Java 中使用 apache cxf 使用 SOAP 服务时被禁止

ras*_*lva 1 java apache soap cxf

在尝试使用 apache cxf 3.1.6 和 java 8 使用 SOAP 服务时,通过更改有效负载内容,我得到了“200 OK”或“403 Forbidden”。

奇怪的是,如果我打印出有效负载并放在 SOAPUI 上,那么我总是得到 200 OK。

你遇到过类似的问题吗?如何解决这个问题?

ras*_*lva 5

所以你不会像我一样陷入困境,挠头思考为什么会发生这种情况,这是问题的解释和解决方案。

出现这个问题是因为默认情况下 apach cxf 使用“分块”,一旦达到定义的阈值,它基本上会向端点发送小块数据。这会导致 SOAP 消息在不完整的情况下被发送,从而导致“403”(假设服务器不支持“分块”)!解决方案是禁用“分块”,通过将 spring-config.xml 更改为:

  <http-conf:conduit name="*.http-conduit">
<http-conf:client Connection="Keep-Alive"
                  MaxRetransmits="1"
                  AllowChunking="false" />
Run Code Online (Sandbox Code Playgroud)

附加信息:

在 org.apache.cxf.transport.http.HTTPConduit 上,对于“准备”方法,有一点可以解释所有内容:

// DELETE does not work and empty PUTs cause misleading exceptions
        // if chunking is enabled
        // TODO : ensure chunking can be enabled for non-empty PUTs - if requested
        if (csPolicy.isAllowChunking() 
            && isChunkingSupported(message, httpRequestMethod)) {
            //TODO: The chunking mode be configured or at least some
            // documented client constant.
            //use -1 and allow the URL connection to pick a default value
            isChunking = true;
            chunkThreshold = csPolicy.getChunkingThreshold();
        }
        cookies.writeToMessageHeaders(message);
Run Code Online (Sandbox Code Playgroud)