美文网首页
httpclient证书锁定 单向认证

httpclient证书锁定 单向认证

作者: 乌托邦缤果 | 来源:发表于2022-08-14 17:06 被阅读0次
public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory() {
        try {
            HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            SSLContext sslcontext = getSslContent2();

            SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE);
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", sslConnectionSocketFactory).build();
            // 开始设置连接池
            PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
            // 最大连接数500
            poolingHttpClientConnectionManager.setMaxTotal(500);
            // 同路由并发数100
            poolingHttpClientConnectionManager.setDefaultMaxPerRoute(100);
            httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
            // 重试次数
            httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(2, true));
            HttpClient httpClient = httpClientBuilder.build();
            // httpClient连接配置
            HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
            // 连接超时
            clientHttpRequestFactory.setConnectTimeout(15000);
            // 数据读取超时时间
            clientHttpRequestFactory.setReadTimeout(30000);
            // 连接不够用的等待时间
            clientHttpRequestFactory.setConnectionRequestTimeout(20000);
            return clientHttpRequestFactory;
        } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
            log.error("初始化HTTP连接池出错", e);
        } catch (Exception e) {
            log.error("初始化HTTP连接池出错", e);
        }
        return null;
    }

    private SSLContext getSslContent1() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        ClassPathResource classPathResource = new ClassPathResource("/cert-test.keystore");
        keyStore.load(classPathResource.getInputStream(), "123456".toCharArray());

        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509");
        trustManagerFactory.init(keyStore);

        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();

        SSLContext sslcontext = SSLContext.getInstance("SSL");

        sslcontext.init(null, trustManagers, null);

        return sslcontext;
    }

    private SSLContext getSslContent2() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                ClassPathResource classPathResource = new ClassPathResource("/cert-test.cer");
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
                Certificate cert = null;
                try {
                    cert = cf.generateCertificate(classPathResource.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                boolean flag = false;
                for (X509Certificate certificate : arg0){
                    if(Objects.equals(cert.getPublicKey(), certificate.getPublicKey())){
                        flag = true;
                        break;
                    }
                }

                return flag;
            }
        }).build();

        return sslContext;
    }

相关文章

  • httpclient证书锁定 单向认证

  • SSL认证与CA认证的区别

    作者:Gakki SSL单向认证与双向认证 SSL单向认证只要求站点部署了ssl证书就行,任何用户都可以去访问(I...

  • Go 实现 TLS server 及client (1, TLS

    本系列文章包含以下内容 单向TLS不认证,客户端不检查服务端证书的有效性 单向TLS认证,客户端检查服务端证书的有...

  • Android HTTPS之自签名证书认证(二)

    使用okhttp框架 双向认证 在上一篇博客中《Android HTTPS之自签名证书认证(一)单向认证》,我们主...

  • 【SSL】SSL双向认证

    双向认证 SSL 协议要求服务器和用户双方都有证书。 单向认证 SSL 协议不需要客户拥有CA证书,具体的过程相对...

  • 2018-03-16

    HTTPS 单向双向认证 自签证书自签名的证书 CA证书 并且上传到github上面✅ 屏幕支持横屏的配置✅视频...

  • SSL双向认证和SSL单向认证的区别

    双向认证 SSL 协议要求服务器和用户双方都有证书。单向认证 SSL 协议不需要客户拥有CA证书,具体的过程相对于...

  • EveryDay模板

    每日问题列表 1、一个故事看懂安全证书 2、SSL和TSL 3、单向认证和双向认证 话题一:一个故事看懂安全证书 ...

  • AFNetworking框架分析(六)——AFSecurityP

    在AF框架中,AFSecurityPolicy类只做了一件事,就是完成HTTPS认证。作为单向认证证书是否合法。先...

  • ssl认证

    单向认证 1.服务端向ca认证机构申请证书,获得公私钥和证书;2.客户端向服务端发送请求;3.服务端向客户端发送证...

网友评论

      本文标题:httpclient证书锁定 单向认证

      本文链接:https://www.haomeiwen.com/subject/dvguirtx.html