我正在尝试使用安全套接字层(HTTPS)在Java中与我的PHP脚本建立连接,但我发现为确保最大的安全性/有效性,我必须将我的网站使用的SSL证书导入到我的应用程序中...这是我不知道如何做的事情。
如果可以的话,我的SSL证书不是自签名的,而是由StartSSL提供的,并且我正在使用Eclipse IDE。
有人能指点我正确的方向吗?例如,我需要哪些文件,应该在哪里导入它们,以及我需要在Java中编写什么代码等等?
我正在尝试使用安全套接字层(HTTPS)在Java中与我的PHP脚本建立连接,但我发现为确保最大的安全性/有效性,我必须将我的网站使用的SSL证书导入到我的应用程序中...这是我不知道如何做的事情。
如果可以的话,我的SSL证书不是自签名的,而是由StartSSL提供的,并且我正在使用Eclipse IDE。
有人能指点我正确的方向吗?例如,我需要哪些文件,应该在哪里导入它们,以及我需要在Java中编写什么代码等等?
Or, you could initialize your application with your own truststore using the -Djavax.net.ssl.trustStore=<path_to_truststore> -Djavax.net.ssl.trustStorePassword=<truststore_password>
JVM startup flags, or execute the following code before initializing SSL connections:
System.setProperty("javax.net.ssl.trustStore","<path_to_truststore>");
System.setProperty("javax.net.ssl.trustStorePassword","<truststore_password>");
This is a viable approach only if your application is a Java SE application that does not happen to be an applet (or an application with similar restrictions on how the truststore is specified).
阅读Java keytool文档也会有所帮助。
keytool
可执行文件,则它在JRE和JDK中都可用,在JAVA_HOME/bin
目录中。因此,如果您已将Java安装在C:\ Java \ jdk1.6.0_26
中,则该目录将是JAVA_HOME
,其中keytool
位于bin
子目录中。 - Vineet Reynoldskeystore
命令的必要性。CACERTS_PASSWORD
。请注意,该方法在添加证书后保存密钥库,因此运行一次后,证书将永久保存在密钥库中。import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
/**
* Add a certificate to the cacerts keystore if it's not already included
*/
public class SslUtil {
private static final String CACERTS_PATH = "/lib/security/cacerts";
// NOTE: DO NOT STORE PASSWORDS IN PLAIN TEXT CODE, LOAD AT RUNTIME FROM A SECURE CONFIG
// DEFAULT CACERTS PASSWORD IS PROVIDED HERE AS A QUICK, NOT-FOR-PRODUCTION WORKING EXAMPLE
// ALSO, CHANGE THE DEFAULT CACERTS PASSWORD, AS IT IMPLORES YOU TO!
private static final String CACERTS_PASSWORD = "changeit";
/**
* Add a certificate to the cacerts keystore if it's not already included
*
* @param alias The alias for the certificate, if added
* @param certInputStream The certificate input stream
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws CertificateException
* @throws IOException
*/
public static void ensureSslCertIsInKeystore(String alias, InputStream certInputStream)
throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException{
//get default cacerts file
final File cacertsFile = new File(System.getProperty("java.home") + CACERTS_PATH);
if (!cacertsFile.exists()) {
throw new FileNotFoundException(cacertsFile.getAbsolutePath());
}
//load cacerts keystore
FileInputStream cacertsIs = new FileInputStream(cacertsFile);
final KeyStore cacerts = KeyStore.getInstance(KeyStore.getDefaultType());
cacerts.load(cacertsIs, CACERTS_PASSWORD.toCharArray());
cacertsIs.close();
//load certificate from input stream
final CertificateFactory cf = CertificateFactory.getInstance("X.509");
final Certificate cert = cf.generateCertificate(certInputStream);
certInputStream.close();
//check if cacerts contains the certificate
if (cacerts.getCertificateAlias(cert) == null) {
//cacerts doesn't contain the certificate, add it
cacerts.setCertificateEntry(alias, cert);
//write the updated cacerts keystore
FileOutputStream cacertsOs = new FileOutputStream(cacertsFile);
cacerts.store(cacertsOs, CACERTS_PASSWORD.toCharArray());
cacertsOs.close();
}
}
}
使用方法如下:
SslUtil.ensureSslCertIsInKeystore("startssl", new FileInputStream("/path/to/cert.crt"));
changeit
)是默认的 cacerts
密码,应该进行更改。我会在代码片段中添加注释来指出这一点。 - Shane显然,由于某些原因,mailgun工程师不想给我们清晰的解决方案。这是我所做的。
我们运行tomcat8并通过jersey web服务连接到mailgun API。我按照这位用户的指示操作,效果很好。希望能帮到有需要的人。
在1月22日,由于Symantec的PKI基础设施被设置为不受信任,我们更新了SSL证书。一些旧版本的Java没有“DigiCert Global Root G2” CA。
有几个选项:
将“DigiCert Global Root G2” CA导入您的“cacerts”文件。
升级您的JRE到8u91(或更高版本),其中包括此根证书。
要导入“DigiCert Global Root G2”,您可以从https://www.digicert.com/digicert-root-certificates.htm下载根证书。请确保下载正确的根证书。
下载证书后,您需要使用以下命令将其导入:
keytool -import -trustcacerts -keystore /path/to/cacerts -storepass changeit -noprompt -alias digicert-global-root-g2 -file /path/to/digicert.crt 您需要设置Java Keystore的路径和您下载的根证书的位置。
所以: 1. /path/to/digicert.crt是您刚下载的文件。 2. /path/to/cacerts - 这在您的JRE路径中。我使用“find / -name cacerts -print”命令快速查找了所有Java cacerts在我的文件系统上的位置。对于我来说,它在/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/security/cacerts中。
javax.net.ssl.keyStore
javax.net.ssl.keyStorePassword
使用keytool工具将SSL证书传递给JVM或导入到JRE密钥库中