如何使用Spring RestTemplate调用HTTPS restful Web服务 [英] How to call HTTPS restful web services using Spring RestTemplate
问题描述
我正在使用Tomcat7,Spring框架进行ReST Web服务。
我尝试使用 Spring RestTemplate 调用 https Web服务。
我收到以下错误:
I am using Tomcat7, Spring framework for ReST web services. I am trying to call an https web service using Spring RestTemplate. I am getting the following error:
无法找到所请求目标的有效证书路径;嵌套的
异常是javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException:PKIX路径构建失败:
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
我在stackoverflow上在线检查。我尝试了网址中的示例代码:
使用Https Rest服务访问Spring RestTemplate
I check online at stackoverflow. I tried the sample code from the url: Access Https Rest Service using Spring RestTemplate
我无法让它工作。
任何人都可以根据下面的代码告诉我我需要更改什么?
也可以有人告诉我或者提供我需要的java库的pom.xml文件吗?
I couldn't get it to work. Can anybody please tell me based on the code below what do I need to change? Also can anybody tell me or provide me with the pom.xml file which java libraries would I need?
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.journaldev.spring.controller.EmpRestURIConstants;
import com.journaldev.spring.model.CostControlPost;
import com.journaldev.spring.model.Employee;
import com.journaldev.spring.model.RfxForUpdate;
import static org.junit.Assert.*;
import org.apache.commons.codec.binary.Base64;
import javax.net.ssl.*;
import java.io.*;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
public class TestExample2
{
public static final String SERVER_LIST="https://abc/sourcing/testServices";
@Test
public void testGetListOfServiceNames()
{
try
{
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(SERVER_LIST,HttpMethod.GET,null,String.class);
assertNotNull(response);
}
catch(Exception e)
{
System.out.println("e:"+e.getMessage());
}
}
}
推荐答案
您需要在密钥库中拥有证书,或者您可以接受所有证书(拒绝证书验证)
Either you need to have certificates in your keystore or you can accept all certificates (kind off ignore certificate validation)
所以你可以将rest模板的bean重新定义为
So you can re-define bean of rest template as
@Bean
public RestTemplate restTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
}
除了apache核心,客户端和依赖项之外,你不需要额外的jar。
You should not need additional jars except for apache core, client and dependencies.
这篇关于如何使用Spring RestTemplate调用HTTPS restful Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!