问题
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