Java Rest call with different user certs

主宰稳场 提交于 2019-12-08 08:57:29

问题


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

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.

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??


回答1:


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

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


来源:https://stackoverflow.com/questions/37065133/java-rest-call-with-different-user-certs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!