반응형
Notice
Recent Posts
Recent Comments
«   2024/11   »
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
Archives
Today
Total
관리 메뉴

차근차근

glide 4 https Image URL Not Working API 16~19 본문

개발

glide 4 https Image URL Not Working API 16~19

철산92 2020. 8. 30. 21:48
반응형

glide 4 https 확인되지 않은 ssl 인증서 이미지url 출력 안되는 문제 

API 16 ~ 19 버전에서 발생하는 문제 

javax.net.ssl.SSLHandshakeException오류

 

 

1. dependencies 추가

build.gradle(:app)

dependencies {

implementation "com.squareup.okhttp3:okhttp:3.8.1"

implementation 'com.github.bumptech.glide:glide:4.11.0'

implementation ('com.github.bumptech.glide:okhttp3-integration:4.11.0'){

exclude group: 'glide-parent'

}

annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

}

 

2. UnsafeOkHttpClient.java,UnsafeOkHttpGlideModule.java 파일생성

UnsafeOkHttpClient.java

import java.security.cert.CertificateException;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;

public class UnsafeOkHttpClient {
public 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(sslSocketFactory, (X509TrustManager)trustAllCerts[0]);
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);
}
}
}

 

UnsafeOkHttpGlideModule.java

import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;

import java.io.InputStream;

import okhttp3.OkHttpClient;

@GlideModule
public class UnsafeOkHttpGlideModule extends AppGlideModule {
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
OkHttpClient client = UnsafeOkHttpClient.getUnsafeOkHttpClient();
registry.replace(GlideUrl.class, InputStream.class,
new OkHttpUrlLoader.Factory(client));
}
}

 

3.AndroidManifest.xml 추가

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

 

4.AndroidStudio -> Build -> Make Moudle 'app' 클릭

 

참조 : https://futurestud.io/tutorials/glide-module-example-accepting-self-signed-https-certificates

 

 

 

반응형
Comments