codecamp

Spring Cloud 放松模板的SSL验证

WireMock允许您使用“ https” URL协议对“安全”服务器进行存根。如果您的应用程序希望在集成测试中联系该存根服务器,它将发现SSL证书无效(自安装证书的常见问题)。最好的选择通常是将客户端重新配置为使用“ http”。如果这不是一种选择,则可以要求Spring配置忽略SSL验证错误的HTTP客户端(当然,仅对测试而言如此)。

为了使此工作最小,您需要在应用中使用Spring Boot RestTemplateBuilder,如以下示例所示:

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
	return builder.build();
}

您需要RestTemplateBuilder,因为构建器是通过回调传递的,以对其进行初始化,因此此时可以在客户端中设置SSL验证。如果您使用的是@AutoConfigureWireMock批注或存根运行程序,则这会在测试中自动发生。如果使用JUnit @Rule方法,则还需要添加@AutoConfigureHttpClient批注,如以下示例所示:

@RunWith(SpringRunner.class)
@SpringBootTest("app.baseUrl=https://localhost:6443")
@AutoConfigureHttpClient
public class WiremockHttpsServerApplicationTests {

	@ClassRule
	public static WireMockClassRule wiremock = new WireMockClassRule(
			WireMockSpring.options().httpsPort(6443));
...
}

如果您使用的是spring-boot-starter-test,则将Apache HTTP客户端放在类路径上,并由RestTemplateBuilder选择它,并将其配置为忽略SSL错误。如果使用默认的java.net客户端,则不需要注释(但不会造成任何危害)。 当前不支持其他客户端,但可能会在将来的版本中添加。

要禁用自定义RestTemplateBuilder,请将wiremock.rest-template-ssl-enabled属性设置为false

Spring Cloud 替代:使用JUnit规则
Spring Cloud WireMock和Spring MVC模拟
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

三、Spring Cloud Netflix

SpringCloud Hystrix超时和Ribbon客户

SpringCloud 重试失败的请求

五、Spring Cloud Stream

六、SpringCloud Binder实现

SpringCloud 重试RabbitMQ Binder

SpringCloud Dead-Letter队列处理

八、Spring Cloud Sleuth

SpringCloud 当前Span

十二、Spring Cloud for Cloud Foundry

十三、Spring Cloud Contract

Spring Cloud Contract验证程序设置

SrpingCloud Gradle项目

十五、Spring Cloud网关

Spring Cloud 配置路由谓词工厂和网关过滤工厂

Spring Cloud TLS / SSL

Spring Cloud网关配置

SpringCloud 故障排除

十八、Spring Cloud GCP

Spring Cloud GCP Spring资源

Spring Cloud Spring JDBC

Spring Cloud Redis的Cloud Memorystore

Spring Cloud 云身份识别代理(IAP)身份验证

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }