Last active
August 29, 2015 13:57
-
-
Save ehdez73/9473407 to your computer and use it in GitHub Desktop.
SSL with RestTemplate to localhost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.net.ssl.HostnameVerifier; | |
| import javax.net.ssl.HttpsURLConnection; | |
| import javax.net.ssl.SSLSession; | |
| import org.springframework.web.client.RestTemplate; | |
| public class Application { | |
| static { | |
| System.setProperty("javax.net.ssl.trustStore", "/opt/cert/trustStore"); | |
| System.setProperty("javax.net.ssl.trustStorePassword", "s3cret"); | |
| // workaround for localhost | |
| HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier() { | |
| @Override | |
| public boolean verify(String hostname, SSLSession sslSession) { | |
| if (hostname.equals("localhost")) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| }); | |
| } | |
| public static void main(String[] args) throws Exception { | |
| RestTemplate restTemplate = new RestTemplate(); | |
| String message = restTemplate.getForObject("https://localhost:8443/myapp/echo", String.class); | |
| System.out.println(message); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment