使用Citrus时如何在服务器证书中禁用主机名验证

时间:2019-02-26 21:16:36

标签: java ssl-certificate citrus-framework

使用Citrus测试框架针对服务器进行测试时,我无法禁用证书检查。

我收到以下错误: org.springframework.web.client.ResourceAccessException:“”的POST请求出现I / O错误:的证书与证书主题的通用名称不匹配:嵌套的异常是javax.net.ssl.SSLException。

以下是用于在Citrus中创建端点的代码段:

if-then-else

任何想法都值得欢迎。

1 个答案:

答案 0 :(得分:0)

我终于找到了一种查看Citrus源代码的方法,这是我想出的使它工作的方法,只是将其放在此处以防其他人使用:

    @Bean
    public HttpComponentsClientHttpRequestFactory mySSLBasicAuthRequestFactory() throws Exception {

        AuthScope authScope = new AuthScope(myServer, myPort, "", "basic"); 
        HttpComponentsClientHttpRequestFactory myHttpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory() {
            @Override
            protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
                // we have to use preemptive authentication
                // therefore add some basic auth cache to the local context
                AuthCache authCache = new BasicAuthCache();
                BasicScheme basicAuth = new BasicScheme();

//                authCache.put(new HttpHost(authScope.getHost(), authScope.getPort(), "http"), basicAuth);
                authCache.put(new HttpHost(authScope.getHost(), authScope.getPort(), "https"), basicAuth);

                BasicHttpContext localcontext = new BasicHttpContext();
                localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

                return localcontext;
            }
        };

        UsernamePasswordCredentials myCredentials = new UsernamePasswordCredentials(myUser, myPassword);
        CredentialsProvider provider = new BasicCredentialsProvider();
        provider.setCredentials(AuthScope.ANY, myCredentials);

        CloseableHttpClient myHttpClient = HttpClients.custom()
                .setSSLHostnameVerifier(new NoopHostnameVerifier())
                .setDefaultCredentialsProvider(provider)
                .setSslcontext(new SSLContextBuilder().loadTrustMaterial(null, (x509Certificates, s) -> true)
                    .build())
                .build();

        myHttpComponentsClientHttpRequestFactory.setHttpClient(myHttpClient);
        return myHttpComponentsClientHttpRequestFactory;
    }