当前位置:   article > 正文

httpClient发送携带pem证书和key文件_http请求设置key和pem证书文件

http请求设置key和pem证书文件

1、jar包

<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.0</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

2、证书
我的pem是通过jks生成的
jks生成key文件


keytool -importkeystore -srckeystore mfi-key.jks -destkeystore server.p12 -srcstoretype jks -deststoretype pkcs12
openssl pkcs12 -in server.p12 -nocerts -nodes -out server.key
  • 1
  • 2
  • 3

3、逻辑代码

public class apple {
    public static void main(String[] args) throws Exception {

		// post body 参数
        String param = "";
       //证书路径
        String pemPath = "";
        //key文件路径
        String keyPath = "";
        SSLSocketFactory sslsf =getSocketFactoryPEM(pemPath, keyPath);
        String body = HttpRequest.post("")
                .setSSLSocketFactory(sslsf)
                .body(param)
                .execute().body();
        System.out.println(body);

    }



    protected static SSLSocketFactory getSocketFactoryPEM(String pemPath, String keypath) throws Exception {
        byte[] pem = fileToBytes(pemPath);
        byte[] pemKey = fileToBytes(keypath);

        byte[] certBytes = parseDERFromPEM(pem, "-----BEGIN CERTIFICATE-----", "-----END CERTIFICATE-----");
        byte[] keyBytes = parseDERFromPEM(pemKey, "-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----");

        X509Certificate cert = generateCertificateFromDER(certBytes);
        RSAPrivateKey key  = generatePrivateKeyFromDER(keyBytes);

        KeyStore keystore = KeyStore.getInstance("JKS");
        keystore.load(null);
        keystore.setCertificateEntry(cert.getSubjectX500Principal().getName(), cert);
         //生成证书的密码,修改为自己的
        keystore.setKeyEntry("key-alias", key, "123456".toCharArray(), new Certificate[] {cert});

        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(keystore, "123456".toCharArray());

        KeyManager[] km = kmf.getKeyManagers();
       //根据你的jdk版本决定是TLS、TLSv1.1、TLSv1.2
        SSLContext context = SSLContext.getInstance("TLSv1.2");
        context.init(km, null, null);
        SSLSocketFactory sslsf = context.getSocketFactory();
        return sslsf;
    }

    public static byte[] parseDERFromPEM(byte[] pem, String beginDelimiter, String endDelimiter) {
        String data = new String(pem);
        String[] tokens = data.split(beginDelimiter);
        tokens = tokens[1].split(endDelimiter);
        return DatatypeConverter.parseBase64Binary(tokens[0]);
    }

    public static RSAPrivateKey generatePrivateKeyFromDER(byte[] keyBytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory factory = KeyFactory.getInstance("RSA");
        return (RSAPrivateKey)factory.generatePrivate(spec);
    }

    public static X509Certificate generateCertificateFromDER(byte[] certBytes) throws CertificateException {
        CertificateFactory factory = CertificateFactory.getInstance("X.509");
        return (X509Certificate)factory.generateCertificate(new ByteArrayInputStream(certBytes));
    }
    
    public static byte[] fileToBytes(String filePath) {
        //传文件名字,自动获取根目录下
        Resource resource = new ClassPathResource(filePath);
        InputStream is = resource.getStream();
        byte[] buffer = null;
        //File file = new File(filePath);

        //FileInputStream fis = null;
        ByteArrayOutputStream bos = null;

        try {
            //fis = new FileInputStream(is);
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = is.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            buffer = bos.toByteArray();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (null != bos) {
                    bos.close();
                }
            } catch (IOException ex) {
            } finally{
                try {
                    if(null!=is){
                        is.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        return buffer;
    }
}

  • 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
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108

如果出现401权限问题,有可能是你证书读取错误,或者body参数传的不对,或者更换httpClient版本

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/481566
推荐阅读
相关标签
  

闽ICP备14008679号