How can a waitfor task ignore SSL certificate issu

2019-08-02 17:57发布

问题:

The Ant snippet below works well in my local environment, considering my app is running on localhost:

<waitfor maxwait="120" maxwaitunit="second" checkevery="1">
    <and>
        <http url="https://localhost:${env.https.port}/${env.context.path}/${url}"/>
    </and>
</waitfor>

But it does not work on a linux test server. It waits 2 minutes even if the application is running on localhost. I verify the created url is valid using an Ant echo task and by running "curl" for it on the server.

On the server when I run:

curl https://localhost:8080/live/index.html 

I get a certification error. But when I run (ignore certificate):

curl -k https://localhost:8080/live/index.html 

It works well.

I am wondering if the Ant script also does not work because of the certification error, and if so, how can I fix it? If not, any suggestions on the Ant script?

回答1:

Here's a possible starting point. This does some of what curl -k does, but from within Ant. As this disables certificate checking it should be used with care in a safe context! You didn't say which certificate error you had, but you can likely extend the below if needed.

<script language="javascript"><![CDATA[

  // Create a trust manager that does not validate certificate chains

  var TrustManagerInterface = Java.type( "javax.net.ssl.X509TrustManager" );
  var X509TrustManager = new TrustManagerInterface() {
    getAcceptedIssuers: function() { return null; },
    checkClientTrusted: function() { },
    checkServerTrusted: function() { },
  };    

  var TrustManagerArrayType = Java.type( "javax.net.ssl.TrustManager[]" );
  var trust_manager_array = new TrustManagerArrayType( 1 );
  trust_manager_array[0] = X509TrustManager;

  var SecureRandomType = Java.type( "java.security.SecureRandom" );
  var secure_random = new SecureRandomType;

  var SSLContextType = Java.type( "javax.net.ssl.SSLContext" );
  var ssl_context = SSLContextType.getInstance( "SSL" );

  ssl_context.init( null, trust_manager_array, secure_random );

  var HttpsURLConnectionType = Java.type( "javax.net.ssl.HttpsURLConnection" );
  HttpsURLConnectionType.setDefaultSSLSocketFactory( ssl_context.getSocketFactory( ) );


  // Do not validate certificate hostnames

  var HostnameVerifierType = Java.type( "javax.net.ssl.HostnameVerifier" );
  var host_verifier = new HostnameVerifierType() {
    verify: function() { return true; }
  };    
  HttpsURLConnectionType.setDefaultHostnameVerifier( host_verifier );
]]>
</script>


<waitfor maxwait="120" maxwaitunit="second" checkevery="2" checkeveryunit="second">
  <http url="https://wrong.host.badssl.com" />
</waitfor>

I used https://badssl.com to test the above.

As an aside, I think the default unit for check every might be sub-second, so recommend you add checkeveryunit.

Above is for use with Nashorn. A Rhino version should also be possible if you have an older version of Java.

Above was derived from this source.