Add @AutoConfigureHttpClient for SSL validation error handling

For tests only users can add this annotation to make the rest template
in their app ignore SSL validation.
This commit is contained in:
Dave Syer
2017-04-21 16:09:08 +01:00
parent 4414ffb333
commit 235281ce9f
10 changed files with 181 additions and 21 deletions

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.wiremock;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
import org.springframework.context.annotation.Import;
/**
* Annotation for test classes that want to install a RestTemplateCustomizer that sets up
* a Spring Boot app to ignore SSL errors. Use only in tests!
*
* @author Dave Syer
*
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(WireMockRestTemplateConfiguration.class)
@PropertyMapping("wiremock.server")
public @interface AutoConfigureHttpClient {
}

View File

@@ -39,6 +39,7 @@ import org.springframework.context.annotation.Import;
@Documented
@Import(WireMockConfiguration.class)
@PropertyMapping("wiremock.server")
@AutoConfigureHttpClient
public @interface AutoConfigureWireMock {
int port() default 8080;

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2016-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.wiremock;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
/**
* @author Dave Syer
*
*/
@Configuration
public class WireMockRestTemplateConfiguration {
@Bean
@ConditionalOnClass(SSLContextBuilder.class)
public RestTemplateCustomizer restTemplateCustomizer() {
return new RestTemplateCustomizer() {
@Override
public void customize(RestTemplate restTemplate) {
HttpComponentsClientHttpRequestFactory factory = (HttpComponentsClientHttpRequestFactory) restTemplate
.getRequestFactory();
factory.setHttpClient(createSslHttpClient());
}
private HttpClient createSslHttpClient() {
try {
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder().loadTrustMaterial(null,
TrustSelfSignedStrategy.INSTANCE).build(),
NoopHostnameVerifier.INSTANCE);
return HttpClients.custom().setSSLSocketFactory(socketFactory)
.build();
}
catch (Exception ex) {
throw new IllegalStateException("Unable to create SSL HttpClient",
ex);
}
}
};
}
}

View File

@@ -18,13 +18,14 @@ package org.springframework.cloud.contract.wiremock;
import javax.net.ssl.HttpsURLConnection;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.ssl.SSLContexts;
import org.junit.Assert;
import org.springframework.util.ClassUtils;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.springframework.util.ClassUtils;
/**
* Convenience factory class for a {@link WireMockConfiguration} that knows how to use

View File

@@ -4,4 +4,4 @@ org.springframework.cloud.contract.wiremock.WireMockApplicationListener
# RestDocs Auto Configuration
org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs=\
org.springframework.cloud.contract.wiremock.restdocs.WireMockRestDocsConfiguration
org.springframework.cloud.contract.wiremock.restdocs.WireMockRestDocsConfiguration