java - Spring 休息模板 : Host name 'localhost' does not match the certificate subject provided by the peer

标签 java spring ssl resttemplate

我像这样使用 RestTemplate 配置:

private RestTemplate createRestTemplate() throws Exception {
        final String username = "admin";
        final String password = "admin";
        final String proxyUrl = "localhost";
        final int port = 443;

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(proxyUrl, port),
                new UsernamePasswordCredentials(username, password));

        HttpHost host = new HttpHost(proxyUrl, port, "https");

        HttpClientBuilder clientBuilder = HttpClientBuilder.create();

        clientBuilder.setProxy(host).setDefaultCredentialsProvider(credsProvider).disableCookieManagement();

        HttpClient httpClient = clientBuilder.build();
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setHttpClient(httpClient);

        return new RestTemplate(factory);
    }

这就是我的方法的工作原理:

public String receiveMessage(String message) {
        try {
            restTemplate = createRestTemplate();
            ObjectMapper mapper = new ObjectMapper();
            Class1 class1 = null;
            String json2 = "";

            class1= mapper.readValue(message, Class1.class);

            Class1 class2 = restTemplate.getForObject(URL_SERVICE_1 + "/class1/findByName?name=" + class1.getName(),
                    Class1.class);
            System.out.println("Server 1 : " + message);
            json2 = mapper.writeValueAsString(class2);

            return "Error - " + json2;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            return e.getMessage();
        }

    }

URL_SERVICE_1 包含 https://localhost

当我尝试调用函数 GET 时,我总是得到这样的返回:

I/O error on GET request for "https://localhost/class1/findByName?name=20-1P": Host name 'localhost' does not match the certificate subject provided by the peer (CN=*.webku-cool.com, OU=EssentialSSL Wildcard, OU=Domain Control Validated); nested exception is javax.net.ssl.SSLPeerUnverifiedException: Host name 'localhost' does not match the certificate subject provided by the peer (CN=*.webku-cool.com, OU=EssentialSSL Wildcard, OU=Domain Control Validated)

我不知道 restTemplate 与 https 的正确设置。我已经尝试了 23 个有关 SSL 设置的引用资料,但都遇到了同样的错误。

1. Reference

2. Reference

3. Reference

最佳答案

由于接受的答案有弃用的代码,这是我发现有用的:

SSLContextBuilder sslcontext = new SSLContextBuilder();
            sslcontext.loadTrustMaterial(null, new TrustSelfSignedStrategy());
            httpclient = HttpAsyncClients.custom().setSSLContext(sslcontext.build()).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                    .build();

关于java - Spring 休息模板 : Host name 'localhost' does not match the certificate subject provided by the peer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37879751/

相关文章:

java - 如何解决 org.apache.commons.dbcp.SQLNestedException

java - 我如何(正确地)在纯 Java 中以高帧率渲染

java - 如何使在我的应用程序中运行 Activiti Modeller 时,定时器之后的任务和脚本任务服务不会导致错误?

java - 数据库文件结构和索引及实现

java - Spring Boot - 使用 ResourceLoader 读取文本文件

java - 不偶尔发送证书

javascript - jQuery ajax 调用 https URL 时出现拒绝访问错误

ssl - AWS CloudFront SSL 证书 - MalformedCertificate 错误

java - 我怎样才能只订阅最后的数据?

java - Double 代替 Float 和 Float 舍入