Skip to content

Instantly share code, notes, and snippets.

@thomastaylor312
Created November 3, 2016 22:14
Show Gist options
  • Save thomastaylor312/80fcb016020e4115aa64320b98fb0017 to your computer and use it in GitHub Desktop.
Save thomastaylor312/80fcb016020e4115aa64320b98fb0017 to your computer and use it in GitHub Desktop.
Disable SSL validation in Groovy
def nullTrustManager = [
checkClientTrusted: { chain, authType -> },
checkServerTrusted: { chain, authType -> },
getAcceptedIssuers: { null }
]
def nullHostnameVerifier = [
verify: { hostname, session -> true }
]
SSLContext sc = SSLContext.getInstance("SSL")
sc.init(null, [nullTrustManager as X509TrustManager] as TrustManager[], null)
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory())
HttpsURLConnection.setDefaultHostnameVerifier(nullHostnameVerifier as HostnameVerifier)
@osvadimos
Copy link

Thank you. Works for me. Using in grails restBuilder for integration tests.

@crummy
Copy link

crummy commented Nov 11, 2018

Tried this in a Jenkins pipeline:

groovy.lang.MissingMethodException: No signature of method: java.util.LinkedHashMap.checkClientTrusted() is applicable for argument types: (null, null) values: [null, null]

@samsonl
Copy link

samsonl commented Feb 12, 2019

In Jenkins you may need to put @NonCPS in front of your function/method to make this work.

@kchandan
Copy link

Mine works. Tested on latest v of jenkins.

@sify21
Copy link

sify21 commented Jan 19, 2020

I have the same error as @crummy. @kchandan What did you change to make this work?

@sify21
Copy link

sify21 commented Jan 19, 2020

I managed to do this in jenkins pipeline by :

  1. put the script above in a @NonCPS function
  2. disable groovy sandbox and call the function

@dsegoviat
Copy link

If you're using okhttp3 client from 3.1.2 onwards, use the following code instead:

import java.security.cert.X509Certificate

def nullTrustManager = [
    checkClientTrusted: { chain, authType ->  },
    checkServerTrusted: { chain, authType ->  },
    getAcceptedIssuers: { -> new X509Certificate[0] }
]

See this commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment