使用外部TSL执行RestTemplate

时间:2018-03-09 12:02:11

标签: java rest ssl spring-boot

我正在尝试使用可用的公共REST端点,如http {SSL} https://jsonplaceholder.typicode.com/posts/1,它返回一个非常简单的json。

我已将RestTemplate配置为:

@Configuration
public class RestTemplateConfig {

    @Value("${https.remote.proxy.host}")
    private String PROXY_URI;

    @Value("${https.remote.proxy.port}")
    private int PROXY_PORT;

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {

        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        Proxy proxy = new Proxy(Proxy.Type.HTTP,
        new InetSocketAddress(PROXY_URI, PROXY_PORT));
        requestFactory.setProxy(proxy);

        return new RestTemplate(requestFactory);
    }
}

我也在chrome https://jsonplaceholder.typicode.com/posts/1时点击挂锁符号⟼有效⟼详细信息⟼复制到文件jsonplaceholder.cer和KeyStore explorer我复制了cacerts(JAVA_HOME / jre / lib / security)。

当我做下一个时:

@Autowired
RestTemplate restTemplate;

@RequestMapping(value="test", method = RequestMethod.GET)
    public Post getPost() {
    Post post = restTemplate.getForObject("http://jsonplaceholder.typicode.com/posts/1", Post.class);
    return post;
} 

我得到了一个成功的回复:

{
    "userId": "1",
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

但是当我对https网址执行相同操作时,我收到了下一个错误:

{
    "timestamp": 1520596009201,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.web.client.ResourceAccessException",
    "message": "I/O error on GET request for \"https://jsonplaceholder.typicode.com/posts/1\": 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",
    "path": "/api/v1/sepa/test/"
}

这也来自Java:

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141) ~[na:1.8.0_161]
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126) ~[na:1.8.0_161]
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280) ~[na:1.8.0_161]
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:392) ~[na:1.8.0_161]

我失踪了什么?执行此测试是因为我必须连接到供应商API。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的RestTemplate似乎有问题。香草RestTemplate对我来说很好。基本调试程序 - 在开始工作之前将其退出。缩小问题范围。

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getRequestFactory().createRequest(new URI("https://jsonplaceholder.typicode.com/posts/1"), HttpMethod.GET).execute();
        System.out.println("Got it");