use of org.apache.http.ssl.SSLContextBuilder in project spring-boot by spring-projects.
the class AbstractServletWebServerFactoryTests method sslDisabled.
@Test
public void sslDisabled() throws Exception {
AbstractServletWebServerFactory factory = getFactory();
Ssl ssl = getSsl(null, "password", "classpath:test.jks");
ssl.setEnabled(false);
factory.setSsl(ssl);
this.webServer = factory.getWebServer(new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello"));
this.webServer.start();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
this.thrown.expect(SSLException.class);
getResponse(getLocalUrl("https", "/hello"), requestFactory);
}
use of org.apache.http.ssl.SSLContextBuilder in project spring-boot by spring-projects.
the class AbstractServletWebServerFactoryTests method sslNeedsClientAuthenticationFailsWithoutClientCertificate.
@Test(expected = IOException.class)
public void sslNeedsClientAuthenticationFailsWithoutClientCertificate() throws Exception {
AbstractServletWebServerFactory factory = getFactory();
addTestTxtFile(factory);
factory.setSsl(getSsl(ClientAuth.NEED, "password", "classpath:test.jks"));
this.webServer = factory.getWebServer();
this.webServer.start();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
getResponse(getLocalUrl("https", "/test.txt"), requestFactory);
}
use of org.apache.http.ssl.SSLContextBuilder in project spring-boot by spring-projects.
the class AbstractServletWebServerFactoryTests method serverHeaderCanBeCustomizedWhenUsingSsl.
@Test
public void serverHeaderCanBeCustomizedWhenUsingSsl() throws Exception {
AbstractServletWebServerFactory factory = getFactory();
factory.setServerHeader("MyServer");
factory.setSsl(getSsl(null, "password", "src/test/resources/test.jks"));
this.webServer = factory.getWebServer(new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello"));
this.webServer.start();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
ClientHttpResponse response = getClientResponse(getLocalUrl("https", "/hello"), HttpMethod.GET, new HttpComponentsClientHttpRequestFactory(httpClient));
assertThat(response.getHeaders().get("Server")).containsExactly("MyServer");
}
use of org.apache.http.ssl.SSLContextBuilder in project spring-boot by spring-projects.
the class EndpointWebMvcAutoConfigurationTests method assertContent.
private void assertContent(String scheme, String url, int port, Object expected) throws Exception {
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
ClientHttpRequest request = requestFactory.createRequest(new URI(scheme + "://localhost:" + port + url), HttpMethod.GET);
try {
ClientHttpResponse response = request.execute();
if (HttpStatus.NOT_FOUND.equals(response.getStatusCode())) {
throw new FileNotFoundException();
}
try {
String actual = StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8"));
if (expected instanceof Matcher) {
assertThat(actual).is(Matched.by((Matcher<?>) expected));
} else {
assertThat(actual).isEqualTo(expected);
}
} finally {
response.close();
}
} catch (Exception ex) {
if (expected == null) {
if (SocketException.class.isInstance(ex) || FileNotFoundException.class.isInstance(ex)) {
return;
}
}
throw ex;
}
}
use of org.apache.http.ssl.SSLContextBuilder in project pact-jvm by DiUS.
the class InsecureHttpsRequest method setupInsecureSSL.
private void setupInsecureSSL() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
TrustStrategy trustStrategy = (chain, authType) -> true;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build();
b.setSSLContext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
HostnameVerifier hostnameVerifier = new NoopHostnameVerifier();
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
b.setConnectionManager(connMgr);
// finally, build the HttpClient;
// -- done!
this.httpclient = b.build();
}
Aggregations