如何在 java 项目中将新的 pfx 证书文件更改为旧的 jks 文件

How to change new pfx cert file to old jks file in java project

有java项目,有jks认证文件。但它是旧的(过期)。现在我必须将其更改为新的 pfx 认证文件。但是我不知道我是怎么做到的。

这是关于当前项目的一些信息;

这是 pom.xml 旧 jks 文件配置

<profile>
            <id>sign-base</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jarsigner-plugin</artifactId>
                        <version>1.2</version>
                        <executions>
                            <execution>
                                <id>sign</id>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>verify</id>
                                <goals>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <verbose>true</verbose>
                            <storepass>OldJKSKeystorePass</storepass>
                            <keypass>OldJKSKeyPass</keypass>
                            <arguments>
                                <argument>-tsa</argument>
                                <argument>http://timestamp.globalsign.com/scripts/timestamp.dll</argument>
                            </arguments>
                            <keystore>${pom.parent.basedir}${file.separator}OldJKSFile.jks</keystore>
                            <alias>1</alias>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

这里有一个java class,名字是"SpecialHttpsClient" extend default httpsClient,它有一个类似的方法。 mykeystore文件在资源包下,我不知道。

private SSLSocketFactory newSslSocketFactory() {
        InputStream in =null;

        try {
            KeyStore trusted = KeyStore.getInstance("JKS");
            in = this.getClass().getResourceAsStream("/mykeystore");
            trusted.load(in, "mykeystorepass".toCharArray());

            SSLSocketFactory sf = new SSLSocketFactory(trusted);
            sf.setHostnameVerifier(sslSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            return sf;
        } catch (Exception e) {
            logger.error("An error was occurred while creating SSLSocketFactory!***************", e);
            return null;
        } finally {
            if(in!=null )
                try {
                    in.close();
                } catch (IOException e) {
                    logger.error("An error was occurred while creating SSLSocketFactory!***************", e);
                }
        }
    }

这是另一个 class,它的名称是 SpecialHttpsConnection,并且有一个类似的方法。我对文档文件一无所知。

private static TrustManagerFactory getTrustManagerFactory() throws Exception {

        if(trustManagerFactory==null) {
            try {
                KeyStore trusted =null;
                trusted = KeyStore.getInstance("JKS");
                InputStream in = SpecialHttpsConnection.class.getResourceAsStream("/document");
                try {
                    trusted.load(in, "T1@ePudf27?wE".toCharArray());
                } finally {
                    in.close();
                }

                trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                trustManagerFactory.init(trusted);

            } catch(Exception e)  {
                logger.error("An error was occurred while creating TrustManagerFactory!***************",e);
                throw e;
            }

        }

        return trustManagerFactory;
    }

我的问题是;我如何更改 "mynewcert.pfx" 旧文件?

这是 jarsigner 的解决方案,可能对某些人有用

打开cmd,输入cd到/your/jre/bin/目录/

输入

keytool -importkeystore -srckeystore "c:\mypfxfolderdirectory\mypfxfile.pfx" -srcstoretype PKCS12 -destkeystore "c:\mypfxfolderdirectory\myjksfile.jks" -deststoretype JKS -srcstorepass pfxFilePass -deststorepass jksFileStorePass -srcalias 1 -destalias 1 -destkeypass jksFileKeyPass -noprompt

之后,您的 jks 文件将使用两个密码创建:store pass、key pass。