带有okhttp的SSL连接-已签名超限,字节= 465

时间:2018-09-21 13:09:44

标签: java ssl keystore okhttp

我正在尝试向REST端点获取http请求。我绑定到okhttp2.7.5,因为我在项目中使用的框架对其进行了定义。我的研究使我进入了以下主题。

Question I based my code on:

所以我的代码目前看起来正确:

OkHttpClient httpClient = new OkHttpClient();

    SSLContext sslContext;
    TrustManager[] trustManagers;

    char[] keyPassword = "123456".toCharArray();
    String keyLocation = "key.pem";
    String host = "https://localhost:";
    Integer port = 8150;


    try {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null);
        InputStream keyStoreData = new FileInputStream(keyLocation);
        BufferedInputStream bis = new BufferedInputStream(keyStoreData);
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        while (bis.available() > 0) {
            Certificate cert = certificateFactory.generateCertificate(bis);
            keyStore.setCertificateEntry(host + port, cert);
        }

        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);
        trustManagers = trustManagerFactory.getTrustManagers();

        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, keyPassword);

        sslContext = SSLContext.getInstance("SSL");
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(),
                new SecureRandom());

        httpClient.setSocketFactory(sslContext.getSocketFactory());
    } catch (NoSuchAlgorithmException | KeyStoreException | UnrecoverableKeyException | KeyManagementException
            | CertificateException | IOException e) {
        throw new RuntimeException(e);
    }

使用它时,出现以下消息错误:

java.lang.RuntimeException: java.security.cert.CertificateParsingException: signed overrun, bytes = 465

我知道我的证书是有效的,因为它可以在邮递员的要求中使用而不会出现任何问题。

最诚挚的帮助您的人

0 个答案:

没有答案