我是春天的新手。我开发了使用 Java 使用证书使用 RESTful 服务的服务这是我的配置类:package configuration;import org.apache.http.client.HttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.ssl.SSLContextBuilder;import org.springframework.boot.web.client.RestTemplateBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.client.ClientHttpRequestFactory;import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;import org.springframework.util.ResourceUtils;import org.springframework.web.client.RestTemplate;import javax.net.ssl.SSLContext;import java.util.function.Supplier;@Configurationpublic class RestClientCertConfig { private char[] allPassword = "allpassword".toCharArray(); @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception { SSLContext sslContext = SSLContextBuilder .create() .loadKeyMaterial(ResourceUtils.getFile("classpath:keystore.jks"), allPassword, allPassword) .loadTrustMaterial(ResourceUtils.getFile("classpath:truststore.jks"), allPassword) .build(); HttpClient client = HttpClients.custom() .setSSLContext(sslContext) .build(); return builder .requestFactory((Supplier<ClientHttpRequestFactory>)new HttpComponentsClientHttpRequestFactory(client)) .build(); }}
添加回答
举报
0/150
提交
取消