有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用不同的用户证书验证Java Rest调用

我有一套用户证书,我想使用各自的用户证书对用户进行身份验证

我将服务器配置为启用用户身份验证。它在浏览器上运行良好。如果有多个用户证书,它会提示我选择需要使用的证书。 我的问题是,我如何在java上做到这一点??我正在使用RestTemplate与服务器通信

如果是单用户证书,我可以将其添加到java密钥存储中并加以利用。如何使用特定的用户证书进行特定的rest通话


共 (1) 个答案

  1. # 1 楼答案

    这里使用的标准术语是“客户端证书”,因此在谷歌上搜索它可能会更幸运,例如“RestTemplate客户端证书”

    下面是一些来自another堆栈溢出答案的复制/粘贴代码:

    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);