Search in sources :

Example 1 with HttpComponentsClientHttpRequestFactory

use of cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory in project today-infrastructure by TAKETODAY.

the class AbstractServletWebServerFactoryTests method sslWantsClientAuthenticationSucceedsWithoutClientCertificate.

@Test
void sslWantsClientAuthenticationSucceedsWithoutClientCertificate() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    addTestTxtFile(factory);
    factory.setSsl(getSsl(ClientAuth.WANT, "password", "classpath:test.jks"));
    this.webServer = factory.getWebServer();
    this.webServer.start();
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = this.httpClientBuilder.get().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory)).isEqualTo("test");
}
Also used : HttpClient(org.apache.http.client.HttpClient) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 2 with HttpComponentsClientHttpRequestFactory

use of cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory in project today-infrastructure by TAKETODAY.

the class AbstractServletWebServerFactoryTests method sslWithCustomSslStoreProvider.

@Test
void sslWithCustomSslStoreProvider() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    addTestTxtFile(factory);
    Ssl ssl = new Ssl();
    ssl.setClientAuth(ClientAuth.NEED);
    ssl.setKeyPassword("password");
    factory.setSsl(ssl);
    SslStoreProvider sslStoreProvider = mock(SslStoreProvider.class);
    given(sslStoreProvider.getKeyStore()).willReturn(loadStore());
    given(sslStoreProvider.getTrustStore()).willReturn(loadStore());
    factory.setSslStoreProvider(sslStoreProvider);
    this.webServer = factory.getWebServer();
    this.webServer.start();
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    loadStore(keyStore, new FileSystemResource("src/test/resources/test.jks"));
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).loadKeyMaterial(keyStore, "password".toCharArray()).build());
    HttpClient httpClient = this.httpClientBuilder.get().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory)).isEqualTo("test");
    then(sslStoreProvider).should(atLeastOnce()).getKeyStore();
    then(sslStoreProvider).should(atLeastOnce()).getTrustStore();
}
Also used : SslStoreProvider(cn.taketoday.framework.web.server.SslStoreProvider) HttpClient(org.apache.http.client.HttpClient) FileSystemResource(cn.taketoday.core.io.FileSystemResource) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) Ssl(cn.taketoday.framework.web.server.Ssl) KeyStore(java.security.KeyStore) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 3 with HttpComponentsClientHttpRequestFactory

use of cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory in project today-infrastructure by TAKETODAY.

the class AbstractServletWebServerFactoryTests method sslGetScheme.

@Test
void sslGetScheme() throws Exception {
    // gh-2232
    AbstractServletWebServerFactory factory = getFactory();
    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();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    assertThat(getResponse(getLocalUrl("https", "/hello"), requestFactory)).contains("scheme=https");
}
Also used : ServletRegistrationBean(cn.taketoday.framework.web.servlet.ServletRegistrationBean) HttpClient(org.apache.http.client.HttpClient) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) ExampleServlet(cn.taketoday.test.web.servlet.ExampleServlet) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 4 with HttpComponentsClientHttpRequestFactory

use of cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory in project today-infrastructure by TAKETODAY.

the class AbstractServletWebServerFactoryTests method sslKeyAlias.

@Test
void sslKeyAlias() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    Ssl ssl = getSsl(null, "password", "test-alias", "src/test/resources/test.jks");
    factory.setSsl(ssl);
    ServletRegistrationBean<ExampleServlet> registration = new ServletRegistrationBean<>(new ExampleServlet(true, false), "/hello");
    this.webServer = factory.getWebServer(registration);
    this.webServer.start();
    TrustStrategy trustStrategy = new SerialNumberValidatingTrustSelfSignedStrategy("3a3aaec8");
    SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build();
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext)).build();
    String response = getResponse(getLocalUrl("https", "/hello"), new HttpComponentsClientHttpRequestFactory(httpClient));
    assertThat(response).contains("scheme=https");
}
Also used : TrustStrategy(org.apache.http.ssl.TrustStrategy) SSLContext(javax.net.ssl.SSLContext) Ssl(cn.taketoday.framework.web.server.Ssl) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ExampleServlet(cn.taketoday.test.web.servlet.ExampleServlet) ServletRegistrationBean(cn.taketoday.framework.web.servlet.ServletRegistrationBean) HttpClient(org.apache.http.client.HttpClient) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 5 with HttpComponentsClientHttpRequestFactory

use of cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory in project today-infrastructure by TAKETODAY.

the class AbstractServletWebServerFactoryTests method testRestrictedSSLProtocolsAndCipherSuites.

protected void testRestrictedSSLProtocolsAndCipherSuites(String[] protocols, String[] ciphers) throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    factory.setSsl(getSsl(null, "password", "src/test/resources/restricted.jks", null, protocols, ciphers));
    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 = this.httpClientBuilder.get().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    assertThat(getResponse(getLocalUrl("https", "/hello"), requestFactory)).contains("scheme=https");
}
Also used : ServletRegistrationBean(cn.taketoday.framework.web.servlet.ServletRegistrationBean) HttpClient(org.apache.http.client.HttpClient) HttpComponentsClientHttpRequestFactory(cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) ExampleServlet(cn.taketoday.test.web.servlet.ExampleServlet) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Aggregations

HttpComponentsClientHttpRequestFactory (cn.taketoday.http.client.HttpComponentsClientHttpRequestFactory)36 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)30 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)30 HttpClient (org.apache.http.client.HttpClient)28 TrustSelfSignedStrategy (org.apache.http.conn.ssl.TrustSelfSignedStrategy)28 Test (org.junit.jupiter.api.Test)28 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 ServletRegistrationBean (cn.taketoday.framework.web.servlet.ServletRegistrationBean)14 ExampleServlet (cn.taketoday.test.web.servlet.ExampleServlet)14 FileSystemResource (cn.taketoday.core.io.FileSystemResource)10 KeyStore (java.security.KeyStore)10 Ssl (cn.taketoday.framework.web.server.Ssl)6 ClientHttpResponse (cn.taketoday.http.client.ClientHttpResponse)4 RestTemplate (cn.taketoday.web.client.RestTemplate)4 InputStreamFactory (org.apache.http.client.entity.InputStreamFactory)3 Compression (cn.taketoday.framework.web.server.Compression)2 SslStoreProvider (cn.taketoday.framework.web.server.SslStoreProvider)2 HttpHeaders (cn.taketoday.http.HttpHeaders)2 HttpMethod (cn.taketoday.http.HttpMethod)2 MediaType (cn.taketoday.http.MediaType)2