본문 바로가기

나의 플랫폼/안드로이드

[Android] SSL Disable 시키기 (Retrofit2, OkHttp3)

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
private RetrofitBuilder(Context context, String url) {
    builder = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .client(getUnsafeOkHttpClient(context, token, SysUtils.getVersionName(context)))
            .build();
}
 
private static OkHttpClient.Builder buildOkHttpClient() {
    OkHttpClient.Builder builder = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            // Customize the request
            Request original = chain.request();
            HttpUrl url = null;
 
            switch (original.method()) {
                case METHOD_GET:
                case METHOD_DELETE:
                    url = original.url()
                            .newBuilder()
                            .addQueryParameter("""")
                            .build();
                    break;
                default:
                    url = original.url();
                    break;
            }
 
 
            Request.Builder builder = original.newBuilder().url(url);
            builder.addHeader("""");
            builder.method(original.method(),original.body());
            Request request = builder.build();
 
            Response response = chain.proceed(request);
            return response;
        }
    });
 
    return builder;
}
 
private static OkHttpClient getUnsafeOkHttpClient(Context context) {
    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 = buildOkHttpClient();
        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);
    }
}
cs



위 소스를 참고 하시면 될 듯 합니다.


참고로 Retrofit2는 2.3.0 버전이며, OkHttp는 3.8.0 버전 입니다.


출처 : https://stackoverflow.com/a/42155739