【问题标题】:Verify Certificate and Hostname for an HTTPS REST call with Spring RestTemplate使用 Spring RestTemplate 验证 HTTPS REST 调用的证书和主机名
【发布时间】:2019-10-23 12:55:40
【问题描述】:

我有一个 Spring Boot 微服务,我试图在其中调用一个公开 HTTPS REST 端点 (TLS v1.2) 的外部服务器。我已获得 .pem 格式的服务器端证书。

我想使用 RestTemplate 实现此调用,并使用提供的证书并在调用期间验证主机名。

我已尝试在 Google 上搜索此内容,但所有搜索结果都试图忽略证书和主机名。

我可以有一个示例代码 sn-p 来正确实现吗?

【问题讨论】:

    标签: spring-boot https ssl-certificate resttemplate tls1.2


    【解决方案1】:

    在对不同的博客和 stackoverflow 线程进行了一些挖掘之后,以下内容对我有用:

    创建休息模板:

    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    keyStore.load(new FileInputStream(ResourceUtils.getFile(clientKeyPath)), "".toCharArray());
    
    SSLContext sslContext = SSLContextBuilder
                    .create()
                    .loadKeyMaterial(keyStore, null)
                    .loadTrustMaterial(ResourceUtils.getFile(keystorePath), keystorePassword.toCharArray())
                    .build();
    
    SSLConnectionSocketFactory sslConnectionSocketFactory = new  SSLConnectionSocketFactory(sslContext, new CustomHostnameVerifier());
    
    HttpClient client = HttpClients
                    .custom()
                    .setSSLSocketFactory(sslConnectionSocketFactory)
                    .build();
    
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    
    requestFactory.setHttpClient(client);
    
    RestTemplate sslRestTemplate = new RestTemplate(requestFactory);
    

    CustomHostnameVerifier 的实现:

    @Component
    public class CustomHostnameVerifier implements HostnameVerifier {
    
        @Value("${dns.name}")
        private String dnsName;
    
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return hostname.equals(dnsName);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-26
      • 2011-05-03
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      相关资源
      最近更新 更多