Skip to content

Commit 654a3c2

Browse files
committed
Add trust all certs post
1 parent 829506a commit 654a3c2

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<p><code>This should NOT be used in production and is generally a very bad practice.</code> Ignoring SSL certs might be acceptable in a non production environment or possibly if you are just scraping a website and not sending any sensitive data. Use with your own judgement.</p>
2+
3+
<p>SSL is great, when it works well. Unfortunately it can often be misconfigured which leads to dirty hacks like this. Knowing that this is a bad practice it can easily overcome the dreaded <code>javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target</code> error. This means the SSL cert's chain was probably not set up correctly or is using a lesser known SSL provider. The proper fix involves downloading the SSL cert and manually installing it into the Java keystore using the keytool. This can be a bit tedious especially if you have many servers. You should take the time to do it properly in production.</p>
4+
5+
<h2 class="anchored">Custom TrustManager</h2>
6+
<p>This code was taken from the following <a href="https://gist.github.com/chalup/8706740">gist</a>. We will basically be creating a TrustManager that doesn't actually verify the certificates and just passes everything (bad!).</p>
7+
{{> templates/src/widgets/code/code-snippet file=httpClient section=httpClient.sections.trustManager}}
8+
9+
<h2 class="anchored">Trust all SSL certificates OkHttpClient</h2>
10+
<p>OkHttpClient conveniently lets you create a new Builder from an existing client. This allows us to take a preconfigured client and just overwrite how it handles SSL.</p>
11+
{{> templates/src/widgets/code/code-snippet file=httpClient section=httpClient.sections.trustAllSslClient}}
12+
<p>That's all there is to it. You should now be able to create a client that trusts all SSL certs as shown below.</p>
13+
<pre class="line-numbers"><code class="language-java">OkHttpClient client = HttpClient.trustAllSslClient(preconfiguredClient);</code></pre>

0 commit comments

Comments
 (0)