-
-
Save aembleton/889392 to your computer and use it in GitHub Desktop.
/** | |
* Disables the SSL certificate checking for new instances of {@link HttpsURLConnection} This has been created to | |
* aid testing on a local box, not for use on production. | |
*/ | |
private static void disableSSLCertificateChecking() { | |
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { | |
public X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
@Override | |
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { | |
// Not implemented | |
} | |
@Override | |
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { | |
// Not implemented | |
} | |
} }; | |
try { | |
SSLContext sc = SSLContext.getInstance("TLS"); | |
sc.init(null, trustAllCerts, new java.security.SecureRandom()); | |
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); | |
} catch (KeyManagementException e) { | |
e.printStackTrace(); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
} |
thanks!
It worked like a charm !! So guys who are looking for a java equivalent for cURL command and bypassing the certificate validation for https using -k flag this is yours !!
Is it working for android lollipop?
Yes, It set to the HttpsURLConnection property. And It will effect in the context of the app.
Thanks!
I added some lines and worked under android 6.
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } });
And It's very important that getAcceptedIssuers() has to return empty array instead of null.
Thanks. Its work like a charm....!!!
where doo put this code?i cant finde HttpURLConnection
how to use this code in httpurlconncetion?
How to re-enable it?
What is the default ? disables SSL or not?
Is there anyone who had used that in react native and could give hints how to accomplish that?
I am working in react-native, how to incorporate this section of code and in which file as I am new to Java too.
Still need this on react-native too! Any idea?
Its not working fine
witch document should I put this code?
Thanks a lot!
in which file should i paste that code?
Great, thanks!
Where to copy paste this code?
Great, thanks!
Where to copy paste this code??
If you're here for NativeScript. You'll need to turn it into a Class, with the correct imports, then put the file in App_Resources/Android/src/main/
I have forked this Gist and ported it to be used in NativeScript. Better instructions are there.
https://gist.github.com/Omnomios/fa606e700c9405b5aa419d26661cb062
this works for me, im using a bareworkflow react native expo
i create a file called IgnoreSSLFactory.kt in the same path as MainApplication.kt
package your package
import com.facebook.react.modules.network.OkHttpClientFactory
import com.facebook.react.modules.network.ReactCookieJarContainer
import okhttp3.OkHttpClient
import java.security.cert.X509Certificate
import java.util.concurrent.TimeUnit
import javax.net.ssl.*
class IgnoreSSLFactory : OkHttpClientFactory {
override fun createNewNetworkModuleClient(): OkHttpClient {
return try {
val trustAllCerts = arrayOf<TrustManager>(
object : X509TrustManager {
override fun checkClientTrusted(chain: Array<X509Certificate?>?, authType: String?) {}
override fun checkServerTrusted(chain: Array<X509Certificate?>?, authType: String?) {}
override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
}
)
val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustAllCerts, java.security.SecureRandom())
val sslSocketFactory = sslContext.socketFactory
val builder = OkHttpClient.Builder()
.connectTimeout(0, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
.writeTimeout(0, TimeUnit.MILLISECONDS)
.cookieJar(ReactCookieJarContainer())
builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
builder.hostnameVerifier { _, _ -> true }
builder.build()
} catch (e: Exception) {
throw RuntimeException(e)
}
}
}
and then i use in MainApplication.kt
override fun onCreate() {
super.onCreate()
SoLoader.init(this, false)
OkHttpClientProvider.setOkHttpClientFactory(IgnoreSSLFactory()); // HERE
if (!BuildConfig.REACT_NATIVE_UNSTABLE_USE_RUNTIME_SCHEDULER_ALWAYS) {
ReactFeatureFlags.unstable_useRuntimeSchedulerAlways = false
}
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// If you opted-in for the New Architecture, we load the native entry point for this app.
load()
}
if (BuildConfig.DEBUG) {
ReactNativeFlipper.initializeFlipper(this, reactNativeHost.reactInstanceManager)
}
ApplicationLifecycleDispatcher.onApplicationCreate(this)
}
then just run gradlew clean, and run your application
Thanks aembleton, I just call it once in my App started working fine.