Java Rest调用具有不同的用户证书 [英] Java Rest call with different user certs

查看:382
本文介绍了Java Rest调用具有不同的用户证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组用户证书,我想使用相应的用户证书对用户进行身份验证。

I have set of user certificates, I would like to authenticate users using respective user cert.

我配置服务器以启用用户身份验证。它从浏览器工作正常。如果有多个用户证书,它会提示我选择需要使用的证书。
我的问题是,我怎么能用java做到这一点?我正在使用RestTemplate与服务器进行通信。

I configured the server to enable user authentication. It works fine from browser. In case of multiple user certs, it prompts me to select the cert need to be used. My question is, how can I do that from java?? I am using RestTemplate to communicate to the server.

如果是单用户证书,我可以将其添加到java密钥库并使用它。如何将特定用户证书用于特定的休息呼叫?

In case of single user certs I can add that to the java key store and make use of it. How can I use a particular user cert for a particular rest call??

推荐答案

这里使用的标准术语是客户端证书,所以你可能会有更多的运气谷歌搜索,例如RestTemplate客户端证书。

The standard terminology to use here are "client certificates", so you would probably have more luck Googling for that, e.g. "RestTemplate client certificate".

以下是来自其他<的一些复制/粘贴代码/ a> Stack Overflow回答:

Here's some copy/pasted code from another Stack Overflow answer:

KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(new File("keystore.jks")),
        "secret".toCharArray());
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
        new SSLContextBuilder()
                .loadTrustMaterial(null, new TrustSelfSignedStrategy())
                .loadKeyMaterial(keyStore, "password".toCharArray()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
        httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
ResponseEntity<String> response = restTemplate.getForEntity(
        "https://localhost:8443", String.class);

这篇关于Java Rest调用具有不同的用户证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆