自签证书是什么
自签证书简单来说就是自己颁发的,不被信任的SSL。它是使用java自己制作出来的证书。通过这种证书升级出来的https网站,在访问的时候会被提示不安全链接,是否继续访问。
碰到这种情况一般都是得我们手动点击,信任继续访问,才能继续。如果通过java代码应该怎么操作呢?我这里只展示关于restTemplate的操作。
编写restTemplate配置文件
package diit.karamay.map.config;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.commons.httpclient.OkHttpClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
// Do any additional configuration here
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();
// 比较关键的是这两句,创建信任自签证书
SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(scsf).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
//先获取到converter列表
List<HttpMessageConverter<?>> converters = builder.build().getMessageConverters();
for(HttpMessageConverter<?> converter : converters){
//因为我们只想要jsonConverter支持对text/html的解析
if(converter instanceof MappingJackson2HttpMessageConverter){
try{
//先将原先支持的MediaType列表拷出
List<MediaType> mediaTypeList = new ArrayList<>(converter.getSupportedMediaTypes());
//加入对text/html的支持
mediaTypeList.add(MediaType.TEXT_HTML);
//将已经加入了text/html的MediaType支持列表设置为其支持的媒体类型列表
((MappingJackson2HttpMessageConverter) converter).setSupportedMediaTypes(mediaTypeList);
}catch(Exception e){
e.printStackTrace();
}
}
}
return restTemplate;
// return builder.build();
// return restTemplate;
}
}