Java SSL: how to disable hostname verification

前端 未结 5 1150
情深已故
情深已故 2020-11-29 01:54

Is there a way for the standard java SSL sockets to disable hostname verfication for ssl connections with a property? The only way I found until now, is to write a hostname

5条回答
  •  情深已故
    2020-11-29 02:17

    It should be possible to create custom java agent that overrides default HostnameVerifier:

    import javax.net.ssl.*;
    import java.lang.instrument.Instrumentation;
    
    public class LenientHostnameVerifierAgent {
        public static void premain(String args, Instrumentation inst) {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String s, SSLSession sslSession) {
                    return true;
                }
            });
        }
    }
    

    Then just add -javaagent:LenientHostnameVerifierAgent.jar to program's java startup arguments.

提交回复
热议问题