Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ApacheHttpClient 4.5和SpringRestTemplate忽略SSL警告_Java_Spring_Ssl_Apache Httpclient 4.x_Hystrix - Fatal编程技术网

Java 使用ApacheHttpClient 4.5和SpringRestTemplate忽略SSL警告

Java 使用ApacheHttpClient 4.5和SpringRestTemplate忽略SSL警告,java,spring,ssl,apache-httpclient-4.x,hystrix,Java,Spring,Ssl,Apache Httpclient 4.x,Hystrix,我们有一个内部服务,我们希望开始通过HTTPS进行呼叫。该服务仅在内部提供,因此我不担心接受所有自签名警告。这就是我正在使用的: BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(REST_USER, REST

我们有一个内部服务,我们希望开始通过HTTPS进行呼叫。该服务仅在内部提供,因此我不担心接受所有自签名警告。这就是我正在使用的:

    BasicCredentialsProvider credentialsProvider =  new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(REST_USER, REST_PASS));
    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadTrustMaterial(null, new TrustStrategy() {
        @Override
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }
    });
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(),
            SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    CloseableHttpClient client = HttpClients.custom()
            .setDefaultCredentialsProvider(credentialsProvider)
            .setSSLSocketFactory(sslsf)
            .build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setHttpClient(client);
    RestTemplate restTemplate =  new RestTemplate(requestFactory);
但我得到了以下错误:

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://localhost/addrgeo/standardUSAddresses": sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
我甚至尝试创建了一个
池客户端连接管理器

    BasicCredentialsProvider credentialsProvider =  new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(REST_USER, REST_PASS));

SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(),SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
        .register("http", new PlainConnectionSocketFactory())
        .register("https", sslsf)
        .build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
cm.setMaxTotal(2000);//max connection

CloseableHttpClient httpClient = HttpClients.custom()
        .setDefaultCredentialsProvider(credentialsProvider)
        .setSSLSocketFactory(sslsf)
        .setConnectionManager(cm)
        .build();

HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
BasicCredentialsProvider凭证提供者=新的BasicCredentialsProvider();
setCredentials(AuthScope.ANY,新用户名密码Credentials(REST_USER,REST_PASS));
SSLContextBuilder=新的SSLContextBuilder();
loadTrustMaterial(空,新TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf=新的SSLConnectionSocketFactory(builder.build(),SSLConnectionSocketFactory.ALLOW\u ALL\u主机名\u验证器);
Registry Registry=RegistryBuilder.create()
.register(“http”,新的PlainConnectionSocketFactory())
.注册(“https”,sslsf)
.build();
PoolightPClientConnectionManager cm=新的PoolightPClientConnectionManager(注册表);
cm.setMaxTotal(2000年)//最大连接
CloseableHttpClient httpClient=HttpClients.custom()
.setDefaultCredentialsProvider(credentialsProvider)
.setSSLSocketFactory(sslsf)
.setConnectionManager(cm)
.build();
HttpComponents客户端HttpRequestFactory requestFactory=新的HttpComponents客户端HttpRequestFactory();
setHttpClient(httpClient);
但我也犯了同样的错误

我们正在用Hystrix来包装我们的呼叫,但我不知道为什么这会对实际呼叫本身产生任何影响

我看了一下这个问题:

但似乎没有一个答案能让我达到我需要的地方

我做错了什么

解决方案 我觉得自己像个傻瓜

结果是IntelliJ出现了一个奇怪的缓存问题。Maven仍在使用旧的源代码。解决方案是关闭IntelliJ,运行
mvn清洁安装
,打开IntelliJ中的缓存并使其失效,然后重新导入项目。为了更好地衡量,我还刷新了
VCS
下的文件状态,并执行了同步