我的JavaWS应用程序将在内部网络中运行,因此将使用公司自己的证书进行签名,并且SSL证书也来自公司的根CA。因此,由于计算机数量巨大,不太可能在每个计算机上手动导入证书。因此,我用一个应自动导入证书的类扩展了我的应用程序,该类可在控制台和IDE中工作,但如果应用程序是由java web start执行的,则不会:



        cf = CertificateFactory.getInstance("X.509");

        signCertIn = ClassLoader.class.getResourceAsStream((pack + signCertName + ".cer"));
        sslCertIn = ClassLoader.class.getResourceAsStream((pack + sslCertName) + ".crt");

        File file = new File(new File(System.getProperty("java.home") + SEP + "lib" + SEP + "security"), "cacerts");
        javaCertIn = new FileInputStream(file);

        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        keystore.load(javaCertIn, passphrase);

        javaCertIn.close();

        if (!keystore.containsAlias(signCertName)) {
            bis = new BufferedInputStream(signCertIn); // <<<<<<< Exception thrown here
            while (bis.available() > 0) {
                Certificate cert = cf.generateCertificate(bis);
                keystore.setCertificateEntry(signCertName, cert);
            }
            save = true;
            signCertIn.close();
        }

        if (!keystore.containsAlias(sslCertName)) {
            bis = new BufferedInputStream(sslCertIn);
            while (bis.available() > 0) {
                Certificate cert = cf.generateCertificate(bis);
                keystore.setCertificateEntry(sslCertName, cert);
            }
            save = true;
            sslCertIn.close();
        }

        if (save) {
            OutputStream out = new FileOutputStream(file);
            keystore.store(out, passphrase);
            out.close();
        }


JavaWS控制台输出

java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.available(Unknown Source)
at at.sviss.util.cert.Certificates.install(Certificates.java:48)
at at.sviss.Main.main(Main.java:91)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javaws.Launcher.executeApplication(Unknown Source)
at com.sun.javaws.Launcher.executeMainClass(Unknown Source)
at com.sun.javaws.Launcher.doLaunchApp(Unknown Source)
at com.sun.javaws.Launcher.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

最佳答案

代替ClassLoader:

signCertIn = ClassLoader.class.getResourceAsStream((pack + signCertName + ".cer"));


使用类本身:

signCertIn = Certificates.class.getResourceAsStream((pack + signCertName + ".cer"));


为我工作...

关于java - KeyStore.load()之后BufferedInputStream将不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24927623/

10-11 12:36