栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在春季,resttemplate发出的每个请求都发送客户证书的正确方法是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在春季,resttemplate发出的每个请求都发送客户证书的正确方法是什么?

这是使用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());}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/497811.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号