栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

http请求,是用hutool,还是封装

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

http请求,是用hutool,还是封装

项目场景

项目需要和第三方产品进行对接,涉及需要通过HttpClient请求第三方接口。
一开始打算封装个工具类,实现HttpClient请求。但是第三方接口需要将部分验证参数放到Header里。
网上大多数文章,没有相关封装,所以采用hutool的HttpRequest来实现。

HttpRequest实现
//地址请求
String data = HttpRequest.get(tokenUrl)
        .header("app_id", app_id)
        .header("app_secret", app_secret)
        .header("grant_type", GRANT_TYPE)
        .header("code", code)
        .header("state", state)
        .header("redirect_uri", redirect_uri)
        .form(params).execute().body();
Bug排查

相关代码已在测试环境调通,可搬到正式环境,请求空的数据,尴尬了。
经排查:报了这个错

SSLHandshakeException: sun.security.validator.ValidatorException: 
PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: 
unable to find valid certification path to requested target
相关文章

据国家信息安全漏洞库介绍,Hutool的的 HttpRequest 被发现忽略所有 TLS/SSL 证书验证。

Hutool的解释:

所以说,工具类一时用一时爽,一直用一直爽,事后火葬场
当然可以用版本去验证解决。我这里采用自己封装个跳过验证的方法。

代码示例
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.net.URI;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class HttpClientUtil {

    static CloseableHttpClient client = null;

    static {
        SSLContext sslcontext = createIgnoreVerifySSL();
        // 设置协议http和https对应的处理socket链接工厂的对象
        Registry socketFactoryRegistry = RegistryBuilder.create()
                .register("http", PlainConnectionSocketFactory.INSTANCE)
                .register("https", new SSLConnectionSocketFactory(sslcontext))
                .build();
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        client = HttpClients.custom().setConnectionManager(connManager).build();
    }

    public static String get(String url,
                                String appId,
                                String appSecret) throws Exception {
        HttpGet httpGet = new HttpGet();
        httpGet.addHeader("app_id", appId);
        httpGet.addHeader("app_secret", appSecret);
        httpGet.setURI(new URI(url));
        CloseableHttpResponse execute = client.execute(httpGet);
        int statusCode = execute.getStatusLine().getStatusCode();
        if (200 != statusCode) {
            return "";
        }
        return EntityUtils.toString(execute.getEntity(), "utf-8");
    }

    public static SSLContext createIgnoreVerifySSL() {
        try {
            SSLContext sc = SSLContext.getInstance("SSLv3");

            // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
            X509TrustManager trustManager = new X509TrustManager() {
                @Override
                public void checkClientTrusted(
                        X509Certificate[] paramArrayOfX509Certificate,
                        String paramString) throws CertificateException {
                }

                @Override
                public void checkServerTrusted(
                        X509Certificate[] paramArrayOfX509Certificate,
                        String paramString) throws CertificateException {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };

            sc.init(null, new TrustManager[]{trustManager}, null);
            return sc;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

}

以上是我在跟第三方接口对接的相关经验。希望这篇文章能对大家有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1035776.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号