这是使用RestTemplate和Apache
HttpClient的方法示例
您应该
RestTemplate使用配置的SSL上下文定义自己的:
@Beanpublic RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception { char[] password = "password".toCharArray(); SSLContext sslContext = SSLContextBuilder.create() .loadKeyMaterial(keyStore("classpath:cert.jks", password), password) .loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(); HttpClient client = HttpClients.custom().setSSLContext(sslContext).build(); return builder .requestFactory(new HttpComponentsClientHttpRequestFactory(client)) .build();} private KeyStore keyStore(String file, char[] password) throws Exception { KeyStore keyStore = KeyStore.getInstance("PKCS12"); File key = ResourceUtils.getFile(file); try (InputStream in = new FileInputStream(key)) { keyStore.load(in, password); } return keyStore;}
现在,此模板执行的所有远程呼叫将使用签名
cert.jks。 注意 :您需要将其
cert.jks放入类路径中
@Autowiredprivate RestTemplate restTemplate;public List<Info> getInfo() throws RestClientException, URISyntaxException { HttpEntity<?> httpEntity = new HttpEntity<>(null, new HttpHeaders()); ResponseEntity<Info[]> resp = restTemplate.exchange( new URI(base_URL + "/Info"), HttpMethod.GET, httpEntity, Info[].class); return Arrays.asList(resp.getBody());}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)