You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JAX-RS/Apache CXF中自定义设置Host请求头

Fixing Custom Host Header Issues in CXF 3.1.4 for SSH-Tunneled HTTPS Services

I’ve run into this exact problem with CXF before! The issue boils down to how CXF handles the Host header by default—it automatically overwrites any manually set Host value with the hostname from your target URI (which is localhost:9443 in your case). Here’s how to fix it:

Why CXF is Overwriting Your Host Header

When you send a request via CXF’s JAX-RS client, the underlying HTTPConduit component generates a Host header based on the URI you’re targeting. Even if you explicitly add a Host header using .header("Host", "my-service.com"), CXF’s automatic header generation will override it unless you disable or modify this behavior.

Step-by-Step Solution

1. Configure the HTTPConduit to Use Your Custom Host

The key is to directly set the Host property on the HTTPConduit’s client configuration. This bypasses CXF’s automatic header generation and ensures your desired Host value is sent.

Here’s a complete code example for a JAX-RS client:

// Initialize your CXF client
Client client = ClientBuilder.newClient();
WebTarget target = client.target("https://localhost:9443/path");

// Get the HTTPConduit from the target
HTTPConduit conduit = WebClient.getConfig(target).getHttpConduit();

// Configure the client to use your custom Host header
conduit.getClient().setHost("my-service.com");

// Verify your TLS settings are correct (you mentioned you already configured this, but just to confirm)
TLSClientParameters tlsParams = conduit.getTlsClientParameters();
if (tlsParams == null) {
    tlsParams = new TLSClientParameters();
}
// Disable CN check since you're using a self-signed certificate
tlsParams.setDisableCNCheck(true);
// Add your custom trust manager for the self-signed cert if needed
// tlsParams.setTrustManagers(new TrustManager[]{yourCustomTrustManager});
conduit.setTlsClientParameters(tlsParams);

// Send the request (no need to manually add the Host header here if you set it via conduit)
Response response = target.request().get();

2. Alternative: Manually Set the Header + Disable Automatic Host Generation

If you prefer to keep the manual header addition, you can disable CXF’s automatic Host header generation by adjusting the autoRedirect property (counterintuitive, but it works in 3.1.x):

// After getting the conduit
conduit.getClient().setAutoRedirect(false);

// Then add your Host header as before
Invocation.Builder builder = target.request()
    .header("Host", "my-service.com");

Response response = builder.get();

3. Spring Configuration (If You’re Using Spring)

If your client is configured via Spring XML, you can set the Host header directly in the HTTPConduit bean:

<bean id="myServiceClient" class="org.apache.cxf.jaxrs.client.WebClient" factory-method="create">
    <constructor-arg value="https://localhost:9443/path"/>
    <property name="config">
        <bean class="org.apache.cxf.jaxrs.client.ClientConfiguration">
            <property name="httpConduit">
                <bean class="org.apache.cxf.transport.http.HTTPConduit">
                    <property name="client">
                        <bean class="org.apache.cxf.transport.http.HTTPConduit$Client">
                            <property name="host" value="my-service.com"/>
                            <property name="disableCNCheck" value="true"/>
                        </bean>
                    </property>
                </bean>
            </property>
        </bean>
    </property>
</bean>

Why This Works

By setting the Host property on the HTTPConduit’s client configuration, you’re telling CXF to use that exact value for the Host header instead of deriving it from the target URI. This matches the behavior of your working curl command, where you explicitly specify the Host header without any automatic override.

内容的提问来源于stack exchange,提问作者lmsurprenant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:07:55