Skip to content

Instantly share code, notes, and snippets.

@ehdez73
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save ehdez73/9473407 to your computer and use it in GitHub Desktop.

Select an option

Save ehdez73/9473407 to your computer and use it in GitHub Desktop.
SSL with RestTemplate to localhost
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