加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

使用springtemplate在春天发出的每个请求发送客户端证书的正确方

发布时间:2020-12-15 01:23:26 所属栏目:大数据 来源:网络整理
导读:我想用我的spring应用程序使用REST服务.要访问该服务,我有一个客户端证书(自签名和.jks格式)进行授权. 对其他服务进行身份验证的正确方法是什么? 这是我的要求: public List 最佳答案 以下是使用RestTemplate和Apache HttpClient执行此操作的示例 您应该使

我想用我的spring应用程序使用REST服务.要访问该服务,我有一个客户端证书(自签名和.jks格式)进行授权.
对其他服务进行身份验证的正确方法是什么?

这是我的要求:

public List
最佳答案
以下是使用RestTemplate和Apache HttpClient执行此操作的示例

您应该使用配置的SSL上下文定义自己的RestTemplate:

@Bean
public 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放入类路径中

@Autowired
private RestTemplate restTemplate;

public List

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读