https://everleaf.blogspot.com/search/label/PHP

6/08/2019

Java class disable verifikasi SSL

Dibawah ini adalah kode java class yang berguna untuk men-disable verification ssl pada java http get / post request dan tambahan untuk postingan sebelumnya Java class simple Http Get dan Java class simple Http Post, simpan kode dibawah ini dengan nama TrustedDomain.java :

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class TrustedDomain {
    static {
        try {
            TrustMe();
        } catch (NoSuchAlgorithmException | KeyManagementException ex) {
            Logger.getLogger(TrustedDomain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static HostnameVerifier TrustMe() throws NoSuchAlgorithmException, KeyManagementException{
        TrustManager[] trustAllCerts = new TrustManager[] {
            new X509TrustManager() {
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }
                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
                
            }
        };
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        return allHostsValid;
    }
}

No comments:

Post a Comment

Diharapkan berkomentar dengan sopan dan santun, terimakasih.