HTTP请求三方接口绕过https证书检查

问题:在http请求https接口过程中经常会遇到SSL证书检查或者证书过期

** sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: validity check failed **
在这里插入图片描述

解决办法:绕过证书检查

代码如下:


public static byte[] getFile(String url) throws  Exception {
        log.info("get file url:"+url);
        try {
            /*HttpClientBuilder builder = HttpClients.custom();
            builder.setSSLHostnameVerifier((hostName, sslSession) -> {
                return true; // 证书校验通过
            });*/
            //CloseableHttpClient httpclient = builder.build();
            //CloseableHttpClient httpclient =  createSSLClientDefault();
            DefaultHttpClient httpclient = new DefaultHttpClient();
            enableSSL(httpclient);
            HttpGet request = new HttpGet(url);
            CloseableHttpResponse response = httpclient.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                if(entity == null) return null;
                byte[] imgByte = EntityUtils.toByteArray(entity);
                return imgByte;
            }

        }catch (Exception ex){
            log.error("get file error:"+ex.getMessage());
            ex.printStackTrace();
            throw ex;
        }
        return null;
    }
    /**
     * 访问https的网站
     * @param httpclient
     */
    private static void enableSSL(DefaultHttpClient httpclient){
        //调用ssl
        try {
            SSLContext sslcontext = SSLContext.getInstance("TLS");
            sslcontext.init(null, new TrustManager[] { truseAllManager }, null);
            SSLSocketFactory sf = new SSLSocketFactory(sslcontext);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            Scheme https = new Scheme("https", sf, 443);
            httpclient.getConnectionManager().getSchemeRegistry().register(https);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 重写验证方法,取消检测ssl
     */
    private static TrustManager truseAllManager = new X509TrustManager(){

        public void checkClientTrusted(
                java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
            // TODO Auto-generated method stub

        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
            // TODO Auto-generated method stub

        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            // TODO Auto-generated method stub
            return null;
        }

    };
};
RSA (Rivest-Shamir-Adleman) 算法是一种非对称加密技术,它包括两个密钥:公钥和私钥。在数字签名中,RSA 被广泛用于保证消息的完整性和发送者的身份。 **RSA签名示例**: 假设你在 Java 中需要使用 RSA 对 HTTP 请求进行签名: 1. **生成密钥对**: - 使用 `java.security.KeyPairGenerator` 类生成一对 RSA 密钥(公钥和私钥)。 ```java KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); // 选择合适的密钥长度 KeyPair keyPair = keyGen.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); ``` 2. **准备数据**: - 将要发送的数据(如 HTTP 请求体)转换为字节数组。 3. **进行签名**: - 使用发送方的私钥对数据进行签名,生成一个数字签名(Signature object)。 ```java Signature signer = Signature.getInstance("SHA256withRSA"); signer.initSign(privateKey); signer.update(data.getBytes(StandardCharsets.UTF_8)); byte[] signatureBytes = signer.sign(); ``` 4. **附上签名**: - 将签名数据附加到原始 HTTP 请求头(通常使用 "Signature" 或 "X-RSA-Signature" 键)。 **验证签名**: 接收端收到请求后,会做类似的操作: 1. **提取数据和签名**: - 从请求中获取数据和签名。 2. **校验签名**: - 使用接收方的公钥解密签名,然后用同样的算法和哈希函数计算原始数据的哈希值。 - 比较接收到的签名和计算出的哈希值,如果匹配,则证明数据未被篡改,且确实来自指定的发送者。 ```java Signature verifier = Signature.getInstance("SHA256withRSA"); verifier.initVerify(publicKey); verifier.update(receivedData.getBytes(StandardCharsets.UTF_8)); boolean isValid = verifier.verify(signatureBytes); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值