原文链接: https://blog.codeleak.pl/2016/02/skip-ssl-certificate-verification-in.html
How to skip SSL certificate verification while using Spring Rest Template? Configure Rest Template so it uses Http Client to create requests.
Note: If you are familiar with sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
the below should help you.
Http Client
Firstly, import HttpClient
(>4.4), to your project
1 | compile('org.apache.httpcomponents:httpclient:4.5.1') |
Configure RestTemplate
Configure SSLContext
using Http Client’s SSLContexts
factory methods:
1 | TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true; |
org.apache.http.ssl.TrustStrategy
is used to override standard certificate verification process. In the above example - it always returns true
, so the certificate can be trusted without further verification.
The Test
1 |
|
Final Word
The above code helps in certain situations (e.g. testing against servers with self-signed certificates), but it should not be used in production - unless you are 100% sure what you are doing.