当前位置: 首页 > 知识库问答 >
问题:

如何使用Spring RestTemboard调用HTTPS restful Web服务

程峻
2023-03-14

我正在使用用于ReST web服务的Springhtml" target="_blank">框架Tomcat7。我正在尝试使用Spring RestTemplate调用https web服务。我得到以下错误:

找不到请求目标的有效认证路径;嵌套的异常是javax。网ssl。例外:太阳。安全验证器。ValidatorException:PKIX路径生成失败:sun。安全供应商。certpath。SunCertPathBuilderException:找不到请求目标的有效证书路径

我在stackoverflow上在线检查。我尝试了url中的示例代码:使用Spring RestTemboard访问Https Rest Service

我无法让它工作。有人能根据下面的代码告诉我我需要更改什么吗?也有人能告诉我或提供我需要哪些java库的pom.xml文件吗?

        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());
                }
            }


        }

共有1个答案

何德寿
2023-03-14

要么你的密钥库中需要有证书,要么你可以接受所有证书(忽略证书验证)

所以您可以将rest模板的bean重新定义为

import javax.net.ssl.SSLContext;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import java.security.cert.X509Certificate;

@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。

 类似资料:
  • 现在请帮助,我想通过SOAP调用一个api,并使用httpclient 4.5.5

  • 我必须使用证书调用服务器API,所以我已经将cer文件导入到密钥库中并完成编码,但仍然得到了javax.net.ssl.SSLHandShakeException:Sun.Security.Validator.ValidatoreXception:PKIX路径构建失败:Sun.Security.Provider.CertPath.SunCertPathBuilderException:找不到请求目

  • MinIO支持采用客户端提供的秘钥(SSE-C)进行S3服务端加密。 客户端必须为SSE-C请求指定三个HTTP请求头: 算法标识符: X-Amz-Server-Side-Encryption-Customer-Algorithm 唯一的合法值是: AES256。 加密秘钥: X-Amz-Server-Side-Encryption-Customer-Key 加密秘钥必须是一个256位的base6

  • 问题内容: 我正在使用GWT作为Web开发框架。我需要从我的GWT客户端代码访问一些REST服务。我还需要解析JSON(或XML),这是这些服务的响应格式。哪个是解决此问题的最佳方法? 提前致谢。 问题答案: 您可以使用标准GWT调用REST服务(或者如果需要在另一个域上调用服务)。 随着JSON响应字符串,你可以调用获得,它可以是一个,等等,这是在所有可用的这个包。

  • 我将以下响应返回给用户 到目前为止,我正在进行三次连续调用来计算这个,每个调用都可以独立于其他调用运行。我尝试制作三种不同的作为: 如果我做了

  • 问题内容: 我正在使用像这样的jquery ajax调用: 通话仍然不会转到网络服务。每当我收到错误警报时。可以帮我一下吗? 问题答案: 尝试使用post作为方法类型,大多数Web服务是受保护的,并且需要使用post而不是Get进行转换 另外还可以帮助您调试错误以及错误提示信息。