HttpClient 调用https接口的三种方式

最近在工作过程中遇到使用httpClient去call https接口失败的问题, 所以就此做了个学习记录, 在这过程中可以通过一下三种方式中的任何一种都解决了上诉问题

1.ignore https certificate

通过在httpClient call https接口之前先调用如下代码即可忽略https认证


private static DefaultHttpClient httpClientTrustingAllSSLCerts() throws NoSuchAlgorithmException, KeyManagementException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, getTrustingManager(), new java.security.SecureRandom());
    SSLSocketFactory socketFactory = new SSLSocketFactory(sc);
    Scheme sch = new Scheme("https", 443, socketFactory);
    httpclient.getConnectionManager().getSchemeRegistry().register(sch);
    return httpclient;
}
private static TrustManager[] getTrustingManager() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws CertificateException {
        }
        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws CertificateException {
        }
        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } };
    return trustAllCerts;
}

2. 通过加载指定证书达到证书校验(在httpClient call之前调用下列代码)

  • 首先需要将对应网站的cer证书从浏览器中导出, 步骤如下
    2.1 点击浏览器URL左侧图标, 然后点击证书

ssl1

2.2 点击详细信息tab, 然后点击"复制到文件选项"

ssl2

2.3 将证书导出为Base64编码格式, 继续点击下一步(比如我们保存名字为myaliyun.cer), 选择保存位置

ssl3

这时候我们就已经将cer导出来了!

2.4 结合之前导出来的certificate文件使用如下命令生成truststore

keytool -import -alias myaliyun -trustcacerts -file myaliyun.cer -keystore myaliyuntrust.jks

然后输入指定密码, 这是我们的trustStore就生成了, 最后就是在JavaCode中指定加载这个trustStore文件(下面keystore和trustStore均为上面我们gen的myaliyuntrust.jks)

  • 通过设置System环境变量加载我们刚刚生成的trust证书
        System.setProperty("javax.net.ssl.keyStore", "keyStore存放位置"));
        System.setProperty("javax.net.ssl.keyStorePassword", "keyStore密码");
        System.setProperty("javax.net.ssl.keyStoreType", "JKS");
        System.setProperty("javax.net.ssl.trustStore", "trustStore存放位置");
        System.setProperty("javax.net.ssl.trustStorePassword", "Trustore密码");
        System.setProperty("javax.net.ssl.trustType", "JKS");

3. 将安全证书导入到java的cacerts证书库(changeit为jdk cecerts证书库的默认密码)

  1. 和2.3步骤一样, 将对应网站的cetificate文件导出来, 这里我将这个证书放到了这个路径下<JAVA_HOME路径>/jre/lib/security/myaliyun.cer
  2. 将刚才导出来的certifate证书导入到java的cacerts证书库,切换到目录 <JAVA_HOME路径>/jre/lib/security, 执行如下命令:

keytool -import -alias myaliyun -keystore cacerts -file ${JAVA_HOME}/jre/lib/security/myaliyun.cer -storepass changeit

然后你会看到如下信息

C:\java\jdk1.8.0_251\jre\lib\security
λ keytool -import -alias myaliyun -keystore cacerts -file C:/java/jdk1.8.0_251/jre/lib/security/myaliyun.cer -storepass changeit
....
.... 
....
#8: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 4C 7A 17 49 7B 8C 31 F0   48 93 A1 FC 83 CA D7 E9  Lz.I..1.H.......
0010: E0 C5 F7 0B                                        ....
]
]

Trust this certificate? [no]:  yes
Certificate was added to keystore

noted: HttpClient 使用例子

dependency

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2.3</version>
</dependency>
            HttpClient httpClient = new DefaultHttpClient();
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000);
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000);
            HttpGet httpGet = null;
            try {
                httpGet = new HttpGet(url);
                HttpResponse response = httpClient.execute(httpGet);
                int httpStatus = response.getStatusLine().getStatusCode();
                if(httpStatus == HttpStatus.SC_OK) {
                    HttpEntity entity = response.getEntity();
                    //to avoid throw exception when new json object, if the contentType is not json, .
                    String contentType = ContentType.getOrDefault(entity).getMimeType();
                    if(contentType.contains("application/json")) {
                        String getJson = EntityUtils.toString(entity);
                        JSONObject jsonObj = new JSONObject(getJson);
                        //the json obj should contains version key
                    }
                    //Consume response content
                    EntityUtils.consume(entity);
                }
            } catch (Exception e) {
		throw e;
            } finally {
                httpGet.abort();
                httpClient.getConnectionManager().shutdown();
            }

总结

个人还是比较推荐使用第三种将安全证书导入到java的cacerts证书库, 因为我们不需要改code, 只需要通过命令就可以了

ssl4
ssl5
还有一点需要注意的是, 我在测试过程中, 需要使用上面三种中任何一种做法的前提是当所访问网站的证书是不安全的时候才需要那么做的. 如果对应网站的证书是安全的, 则不需要上诉做法。 或者还有一种情况就是: 如果我们时候用的httpClient是3.x版本的, 那么无论这个网站的证书是否安全都需要我们添加上面三种方式中的其中一种了

  • 0
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: Spring Boot中使用HttpClient调用第三方HTTPS接口,并忽略SSL证书验证,可以通过以下步骤来实现: 1. 导入HttpClient和SSL相关的依赖: 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> <dependency> <groupId>javax.net.ssl</groupId> <artifactId>javax.net.ssl.HttpsURLConnection</artifactId> <version>1.0.0</version> </dependency> ``` 2. 创建忽略SSL验证的HttpClient对象: ```java import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.HttpClients; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; public class HttpClientUtil { public HttpClient createIgnoreSSLHttpClient() throws Exception { SSLContext sslContext = SSLContext.getInstance("TLS"); X509TrustManager trustManager = new X509TrustManager() { public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException { } public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException { } public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.X509Certificate[0]; } }; sslContext.init(null, new TrustManager[]{trustManager}, null); SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); RequestConfig reqConfig = RequestConfig.custom().setSocketTimeout(120 * 1000).setConnectTimeout(120 * 1000).build(); HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).setDefaultRequestConfig(reqConfig).build(); return httpClient; } } ``` 3. 使用创建的HttpClient对象发送HTTPS请求: ```java import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; public class HttpsClientExample { public static void main(String[] args) throws Exception { HttpClientUtil httpClientUtil = new HttpClientUtil(); CloseableHttpClient httpClient = (CloseableHttpClient) httpClientUtil.createIgnoreSSLHttpClient(); HttpGet httpGet = new HttpGet("https://example.com/api"); CloseableHttpResponse response = httpClient.execute(httpGet); String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); System.out.println(responseBody); response.close(); httpClient.close(); } } ``` 以上就是使用Spring Boot中的HttpClient实现忽略SSL证书的步骤。总结起来,主要包括导入相关依赖,创建忽略SSL验证的HttpClient对象,以及使用该对象发送HTTPS请求。 ### 回答2: Spring Boot中使用HttpClient调用第三方HTTPS接口时,如果忽略SSL证书验证,可以按照以下方法进行操作。 首先,需要在Spring Boot的配置文件application.properties中添加以下配置: ```plaintext # 忽略SSL证书验证 spring.main.allow-bean-definition-overriding=true ``` 然后,创建一个自定义的HttpClientConfig类,用于配置并创建HttpClient对象: ```java import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.net.ssl.SSLContext; @Configuration public class HttpClientConfig { @Value("${httpclient.ssl.ignore-ssl}") private boolean ignoreSSL; @Bean @ConditionalOnProperty(name = "httpclient.ssl.ignore-ssl", havingValue = "true") public HttpClient httpClient() throws Exception { if (ignoreSSL) { SSLContext sslContext = SSLContextBuilder.create() .loadTrustMaterial((chain, authType) -> true) .build(); SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); return HttpClients.custom() .setSSLSocketFactory(sslConnectionSocketFactory) .setDefaultRequestConfig(requestConfig()) .build(); } else { return HttpClients.createDefault(); } } private RequestConfig requestConfig() { return RequestConfig.custom() .setConnectTimeout(5000) .setSocketTimeout(5000) .build(); } } ``` 最后,在需要调用第三方HTTPS接口的地方注入HttpClient对象,并使用该对象进行接口调用即可: ```java import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class HttpService { @Autowired private HttpClient httpClient; public String getResponse(String url) throws Exception { HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); return EntityUtils.toString(httpResponse.getEntity()); } } ``` 以上就是使用Spring Boot的HttpClient调用第三方HTTPS接口并忽略SSL证书验证的方法。请注意,忽略SSL证书验证可能存在安全风险,建议在生产环境中谨慎使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值