Java:接受自签名证书
我使用以下 TrustManager 在 Java 应用程序中接受来自本地测试服务器的自签名证书:
public class CertificateAcceptor {
private TrustManager[] createTrustManager() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// leave blank to trust every client
}
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// leave blank to trust every client
}
}};
return trustAllCerts;
}
我对此有一些安全问题,因为据我所知,它只接受所有证书。所以我问自己是否有办法只接受来自端口 9443 上的本地主机的证书?
I use the following TrustManager to accept self-signed certificates from my local test server in my Java application:
public class CertificateAcceptor {
private TrustManager[] createTrustManager() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// leave blank to trust every client
}
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// leave blank to trust every client
}
}};
return trustAllCerts;
}
I have some security concerns with that, since as far as I know, this accepts just all certificates. So I'm asking myself if there is a way to only accept certificates that are coming from localhost on Port 9443?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设这些是您的自签名证书,而不仅仅是任何自签名证书,那么创建您自己的证书颁发机构可能更有意义,这样您就可以拥有有效的签名证书,而不是创建解决方法并失去拥有证书的好处。
通过创建您自己的 CA,您可以签署自己的证书,然后只需将 CA 证书导入到您的 Java 密钥库中即可。那么自签名证书就不再有问题了。另外,您还可以获得能够信任证书的额外好处(假设您妥善保管您的证书密钥)。
成为您自己的 CA 实际上比您想象的要容易得多。至少对我来说是这样。
这是包含一些分步说明的链接。
http://sandbox.rulemaker.net/ngps/m2/howto.ca.html
Assuming that these are your self-signed certificates and not just any self-signed certificates, it would probably make better sense to create your own certificate authority so you can have valid, signed certificates instead of creating work arounds and losing the benefits of have certificates.
By creating your own CA you can sign your own certificates and then simply import the CA certificate into your Java keystore. Then no more problems with self-signed certs. Plus you have the added benefit of being able to trust the certificate (assuming you take good care of your certificate keys).
Being your own CA is actually much easier than you might think. At least that was the case for me.
Here is a link with some step-by-step instructions.
http://sandbox.rulemaker.net/ngps/m2/howto.ca.html