In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to add RestTemplate HTTPS certificate, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Download RestTemplate add HTTPS certificate certificate
First, save the unsigned certificate locally through the browser, click insecure-> Certificate-> details-> copy to a file, then choose a file name by default and save it. For example, I saved the certificate on my desktop and named it xx.cer.
Certificate Import JDK
If you want to use a certificate in the project, you need to import the certificate into the certificate management of JDK. The import command is as follows:
Keytool-import-noprompt-trustcacerts-alias xx-keystore / home/oracle/jdk1.8.0_181/jre/lib/security/cacerts-file xx.cer
Explain the above command. This command is executed in the linux server. When executing this command, open the terminal under the folder where the certificate is located, and then name the alias. The alias had better be the same as the certificate name, such as xx. In addition, change the JDK path in the above command to your actual path.
Enter the above command after typing, and you will be asked to write a password or something. Just write changeit. If changeit doesn't work, just write changeme chageit.
Generate keystore file
Can I just import the certificate into JDK? What I verify here is not allowed, and the corresponding keystore file must also be generated.
Keystore file generation command: keytool-import-file xx.cer-keystore xx.keystore
Give an explanation to the above command, which is also executed under linux, of course, under windows, and is also executed in the folder where the certificate is located. If you are prompted that the permission is not enough, add sudo, and windows will be executed as an administrator.
After you enter, you will be asked to enter your password, so you can enter chageit accordingly.
After execution, another xx.keystore file will be generated under the current path.
Configuration in the project
Copy the xx.keystore file uploaded above to the classpath of your project
Copy the following restTemplate configuration to your project, in which a httpConverter is used for json format conversion, which has nothing to do with HTTPS. If it is not necessary, delete it and related code.
Package com.abc.air.config;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.security.KeyManagementException;import java.security.KeyStore;import java.security.KeyStoreException;import java.security.NoSuchAlgorithmException;import java.security.cert.X509Certificate;import java.util.ArrayList;import java.util.List;import org.apache.http.config.Registry;import org.apache.http.config.RegistryBuilder;import org.apache.http.conn.socket.ConnectionSocketFactory;import org.apache.http.conn.socket.PlainConnectionSocketFactory Import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.conn.ssl.SSLConnectionSocketFactory;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;import org.apache.http.ssl.SSLContextBuilder;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ClassPathResource Import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;import org.springframework.web.client.RestTemplate;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;/** * Created by ZhaoTengchao on 2019-4-12. * / @ Configurationpublic class RestTemplateConfig {@ Autowired private FastJsonHttpMessageConverter httpMessageConverter @ Bean RestTemplate restTemplate () throws Exception {HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory (); factory.setConnectionRequestTimeout (5 * 60 * 1000); factory.setConnectTimeout (5 * 60 * 1000); factory.setReadTimeout (5 * 60 * 1000) / / https SSLContextBuilder builder = new SSLContextBuilder (); KeyStore keyStore = KeyStore.getInstance (KeyStore.getDefaultType ()); ClassPathResource resource = new ClassPathResource ("nonghang.keystore"); InputStream inputStream = resource.getInputStream (); keyStore.load (inputStream, null); SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory (builder.build (), NoopHostnameVerifier.INSTANCE) Registry registry = RegistryBuilder.create () .register ("http", new PlainConnectionSocketFactory ()) .register ("https", socketFactory) .build (); PoolingHttpClientConnectionManager phccm = new PoolingHttpClientConnectionManager (registry); phccm.setMaxTotal CloseableHttpClient httpClient = HttpClients.custom (). SetSSLSocketFactory (socketFactory) .setConnectionManager (phccm) .setConnectionManagerShared (true). Build (); factory.setHttpClient (httpClient); RestTemplate restTemplate = new RestTemplate (factory); List > convertersValid = new ArrayList () For (HttpMessageConverter converter: converters) {if (converter instanceof MappingJackson2HttpMessageConverter | | converter instanceof MappingJackson2XmlHttpMessageConverter) {continue;} convertersValid.add (converter);} convertersValid.add (httpMessageConverter); restTemplate.setMessageConverters (convertersValid); inputStream.close () Return restTemplate;}}
This is the end of the configuration!
RestTemplate accesses HTTPS
This article briefly describes how to use restTemplate to access https.
Maven org.apache.httpcomponents httpclient 4.5.3
Httpclient's factory is used here
Configure @ Bean public RestTemplate restTemplate () throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {TrustStrategy acceptingTrustStrategy = (X509Certificate [] chain, String authType)-> true; SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom () .loadTrustMaterial (null, acceptingTrustStrategy) .build (); SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory (sslContext) CloseableHttpClient httpClient = HttpClients.custom () .setSSLSocketFactory (csf) .build (); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory (); requestFactory.setHttpClient (httpClient); RestTemplate restTemplate = new RestTemplate (requestFactory); return restTemplate;} verify @ Test public void testHttps () {String url = "https://free-api.heweather.com/v5/forecast?city=CN101080101&key=5c043b56de9f4371b0c7f8bee8f5b75e";" String resp = restTemplate.getForObject (url, String.class); System.out.println (resp);} these are all the contents of the article "how to add HTTPS certificates to RestTemplate". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.