X509Tests.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2002-2018 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package example;
  17. import java.security.KeyStore;
  18. import javax.net.ssl.SSLContext;
  19. import javax.net.ssl.SSLHandshakeException;
  20. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  21. import org.apache.http.impl.client.CloseableHttpClient;
  22. import org.apache.http.impl.client.HttpClients;
  23. import org.apache.http.ssl.SSLContexts;
  24. import org.junit.jupiter.api.Test;
  25. import org.springframework.core.io.ClassPathResource;
  26. import org.springframework.http.ResponseEntity;
  27. import org.springframework.http.client.ClientHttpRequestFactory;
  28. import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
  29. import org.springframework.web.client.RestTemplate;
  30. import static org.assertj.core.api.Assertions.assertThat;
  31. import static org.assertj.core.api.Assertions.assertThatCode;
  32. /**
  33. * Test the Hello World application.
  34. *
  35. * @author Michael Simons
  36. */
  37. public class X509Tests {
  38. @Test
  39. void notCertificateThenSslHandshakeException() {
  40. RestTemplate rest = new RestTemplate();
  41. assertThatCode(() -> rest.getForEntity("https://localhost:8443/", String.class))
  42. .hasCauseInstanceOf(SSLHandshakeException.class);
  43. }
  44. @Test
  45. void certificateThenStatusOk() throws Exception {
  46. ClassPathResource serverKeystore = new ClassPathResource("/certs/server.p12");
  47. KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  48. keyStore.load(serverKeystore.getInputStream(), "password".toCharArray());
  49. // @formatter:off
  50. SSLContext sslContext = SSLContexts.custom()
  51. .loadKeyMaterial(keyStore, "password".toCharArray(), (aliases, socket) -> "client")
  52. .loadTrustMaterial(keyStore, null)
  53. .build();
  54. SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext,
  55. new String[]{"TLSv1.2", "TLSv1.1"},
  56. null,
  57. SSLConnectionSocketFactory.getDefaultHostnameVerifier());
  58. // @formatter:on
  59. CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
  60. ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
  61. RestTemplate rest = new RestTemplate(requestFactory);
  62. ResponseEntity<String> responseEntity = rest.getForEntity("https://localhost:8443/me", String.class);
  63. assertThat(responseEntity).extracting((result) -> result.getStatusCode().is2xxSuccessful()).isEqualTo(true);
  64. }
  65. }