Springboot版本:6.x, HttpClient版本:5.x
需求:除了要忽略SSL证书,还要在请求头里加参数。
xml
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
java
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.HttpRequestInterceptor;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.hc.core5.ssl.TrustStrategy;
public class SSLIgnoreConfig{
/**
* 创建忽略证书的请求工厂
*/
public static ClientHttpRequestFactory insecureRequestFactory()
throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
// 信任所有证书的策略
TrustStrategy acceptingTrustStrategy = (chain, authType) -> true;
// 创建 SSLContext
SSLContext sslContext = SSLContextBuilder.create()
.loadTrustMaterial(acceptingTrustStrategy)
.build();
// 创建 SSL 连接套接字工厂
SSLConnectionSocketFactory sslSocketFactory =
new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
// 创建连接管理器
PoolingHttpClientConnectionManager connectionManager =
PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(sslSocketFactory)
.build();
HttpRequestInterceptor httpRequestInterceptor = (request, entity, context) -> {
// 关键:在 HttpClient 层面添加额外的头信息
request.setHeader("key", "47953053953");
request.setHeader("secret", "dsbkflsehgwlledksv");
};
// 创建 HttpClient
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setConnectionManagerShared(true) // 共享连接管理器
.evictExpiredConnections() // 清理过期连接
.evictIdleConnections(Duration.ofSeconds(30)) // 清理空闲连接
.build();
// 创建 RequestFactory
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory(httpClient);
return requestFactory;
}
}
less
@Configuration
public class RestTemplateConfig {
//关键:使用RestTemplateBuilder,不要用new RestTemplate()
@Bean
@Profile("test")//仅测试环境生效
public RestTemplate restTemplate(RestTemplateBuilder builder) {
ClientHttpRequestFactory requestFactory = SSLIgnoreConfig.insecureRequestFactory();
return builder.requestFactory(()->requestFactory).build;
}
}
csharp
private void testPostRequest() {
System.out.println("\n=== Testing POST Request ===");
HttpHeaders headers = new HttpHeaders();
headers.set("key", "test-key");
headers.set("secret", "test-secret");
headers.setContentType(MediaType.APPLICATION_JSON);
String requestBody = "{"test":"data"}";
HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
try {
ResponseEntity<String> response = restTemplate.exchange(
"https://httpbin.org/post",
HttpMethod.POST,
entity,
String.class
);
System.out.println("POST Response: " + response.getBody());
} catch (Exception e) {
System.err.println("POST Error: " + e.getMessage());
}
}