차근차근
glide 4 https Image URL Not Working API 16~19 본문
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
'개발' 카테고리의 다른 글
[Firebase Cloud Firestore] 스프링 파이어베이스 데이터 베이스 연동 예제 (0) | 2020.09.26 |
---|---|
egov 프로젝트 세팅 (0) | 2020.09.16 |
맥 키보드 한/영 단축키 변환 (0) | 2020.08.05 |
이클립스 캐멀스트링 플러그인, 낙타표기법 단축키 (0) | 2020.03.08 |
자바 리스트 버블정렬 (0) | 2020.02.12 |