• Java跳过证书访问HTTPS


    Java跳过证书访问HTTPS


    java直接发送请求访问https地址的时候,若没有导入证书,会出现各种问题,如307。

    以下会以是否SpringBoot来解决这个问题,做法一致,都是绕过证书进行处理的。

    一,非Spring方式

    创建一个请求代理类,为所有的HTTPS请求访问前做一下操作

    public class IgnoreHttpsProxyRequest {
    
    	/**
    	 * 通過HTTPS的url登錄
    	 * @param urlStr 目標url
    	 * @return	查詢結果
    	 * @throws IOException
    	 * @throws NoSuchAlgorithmException
    	 * @throws KeyManagementException
    	 */
    	public String get(String urlStr, String token, String type) throws IOException, NoSuchAlgorithmException, KeyManagementException {
    		//繞過https
    		HttpsURLConnection.setDefaultHostnameVerifier(new IgnoreHttpsProxyRequest().new NullHostNameVerifier());
    		SSLContext sslContext = SSLContext.getInstance("TLS");
    		sslContext.init(null, trustManagers, new SecureRandom());
    		HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    		//建立連接
    		URL url = new URL(urlStr);
    		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    		connection.setRequestMethod(type);
    		connection.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + token);
    		connection.connect();
    		//獲取查詢結果
    		InputStream inputStream = connection.getInputStream();
    		if (inputStream == null) {
    			return null;
    		}
    		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    		StringBuilder sb = new StringBuilder();
    		String tmp = null;
    		while ((tmp = bufferedReader.readLine()) != null) {
    			sb.append(tmp);
    		}
    		bufferedReader.close();
    		inputStream.close();
    		return sb.toString();
    
    	}
    
    	static TrustManager[] trustManagers = new TrustManager[] {
    				new X509TrustManager() {
    					@Override
    					public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    
    					}
    
    					@Override
    					public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
    
    					}
    
    					@Override
    					public X509Certificate[] getAcceptedIssuers() {
    						return null;
    					}
    				}
    		};
    
        public class NullHostNameVerifier implements HostnameVerifier {
    
    		@Override
    		public boolean verify(String s, SSLSession sslSession) {
    			return true;
    		}
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    二,SpringBoot方式

    先创建一个跳过证书验证,信任所有站点的请求客户端factory

    package com.foxconn.dsc.matrix.api;
    
    import org.springframework.http.client.SimpleClientHttpRequestFactory;
    
    import javax.net.ssl.*;
    import java.io.IOException;
    import java.net.HttpURLConnection;
    import java.security.SecureRandom;
    import java.security.cert.X509Certificate;
    
    /**
     * @ClassName: SkipHttpsRequestFactory
     * @Description:
     * @author: lemon
     * @date: 2023/9/14 13:56
     */
    public class SkipHttpsRequestFactory extends SimpleClientHttpRequestFactory {
    
        @Override
        protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
            if (connection instanceof HttpsURLConnection) {
                prepareHttpsConnection((HttpsURLConnection) connection);
            }
            super.prepareConnection(connection, httpMethod);
        }
    
        private void prepareHttpsConnection(HttpsURLConnection connection) {
            connection.setHostnameVerifier(new SkipHostnameVerifier());
            try {
                connection.setSSLSocketFactory(createSslSocketFactory());
            } catch (Exception ex) {
                // Ignore
            }
        }
    
        private SSLSocketFactory createSslSocketFactory() throws Exception {
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, new TrustManager[] { new SkipX509TrustManager() }, new SecureRandom());
            return context.getSocketFactory();
        }
    
        private class SkipHostnameVerifier implements HostnameVerifier {
    
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
    
        }
    
        private static class SkipX509TrustManager implements X509TrustManager {
    
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
    
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) {
            }
    
            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) {
            }
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    注入RestTemplate类时,构造时将该工厂类加上。

        @Bean
        public RestTemplate restTemplate() {
            SimpleClientHttpRequestFactory factory = new SkipHttpsRequestFactory();
            RestTemplate restTemplate = new RestTemplate(factory);
            return restTemplate;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    使用时将其注入

    @Resource
    private RestTemplate restTemplate;
    
    • 1
    • 2

    配置完毕之后,就可以直接调用了

    ResponseEntity<String> response = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, entity, String.class);
    
    • 1
  • 相关阅读:
    如何在没有第三方.NET库源码的情况,调试第三库代码?
    debian 10 安装apache2 zabbix
    自动化之python面试
    excel表格损坏如何修复?
    “知感冒防流感”全民科普公益行9月10日走进中山
    C++学习 --queue
    解析xml文件
    多线程-- 并发List\队列\Map
    不精确微分/不完全微分(Inexact differential/Imperfect differential)
    静态路由 网络实验
  • 原文地址:https://blog.csdn.net/lmchhh/article/details/132897314