代码之家  ›  专栏  ›  技术社区  ›  Madhu

httpclient中的ssl

  •  0
  • Madhu  · 技术社区  · 7 年前

    我使用的是apache通用版的httpclient api(通用httpclient),实现起来似乎很直接。例如,以简单的方式指定代理,并且在读取网页内容(http或https)时没有区别

    如果我将httpclient版本升级到4.5.x,我发现很难实现相同的需求。 example

    我的问题是,在httpclient 4.5.x的情况下,为什么我们需要ssl/keystore?我需要做什么才能生成新的密钥库以使其工作?

    2 回复  |  直到 7 年前
        1
  •  1
  •   ok2c    7 年前

    我的问题是,在httpclient 4.5.x的情况下,为什么我们需要ssl/keystore?我需要做什么才能生成新的密钥库以使其工作?

    为了你自己你 应该 了解应用程序在建立安全出站连接时应使用的信任/密钥材料。不需要生成新的密钥库,ssl就可以工作。可以简单地使用java运行时附带的信任材料。

        2
  •  0
  •   Nitty Sharma    7 年前
    First if you are consuming service you need to ask or download ssl certificate from server, Generated certificate will not work.
    After that in you https call you need to pass this certificate.
    
    It is not recommended but you can also bypass ssl by following code snippet. Benefit of that is if in future certificate will change you don't need to change this in your client application.
    
    private static OkHttpClient getUnsafeOkHttpClient() {
            try {
                // Create a trust manager that does not validate certificate chains
                final TrustManager[] trustAllCerts = new TrustManager[]{
                        new X509TrustManager() {
                            @Override
                            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                            }
    
                            @Override
                            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
                            }
    
                            @Override
                            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                                return new java.security.cert.X509Certificate[]{};
                            }
                        }
                };
    
                // Install the all-trusting trust manager
                final SSLContext sslContext = SSLContext.getInstance("SSL");
                sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
                // Create an ssl socket factory with our all-trusting manager
                final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
                OkHttpClient.Builder builder = new OkHttpClient.Builder();
    //            builder.sslSocketFactory(getSSLConfig(MyApplication.getAppContext()).getSocketFactory()); //commented to check ssl with certificate
                builder.sslSocketFactory(sslSocketFactory);
                builder.hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                });
                OkHttpClient okHttpClient = builder.build();
                return okHttpClient;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }