使用密钥商店后的JavaMail问题
-
26-12-2019 - |
题
我目前正在处理的JSF应用程序有一个奇怪的问题。看起来我的Programm的两部分是碰撞的。
有两部分:
- “银行”功能
- 邮件功能
银行功能的相关部分(这是练习的假银行):
String path = FacesContext.getCurrentInstance().getExternalContext() .getRealPath("/") + "/WEB-INF/sec/certs.jks";
ErrorHandler.trace(path);
System.setProperty("javax.net.ssl.trustStore", path);
.
在这里,它将信托存储与银行服务器的证书设置。
mail 部分如下所示:
Properties props = new Properties();
props.put("mail.smtp.auth", this.smtpServer.isAuthenticated());
props.put("mail.smtp.starttls.enable", this.smtpServer.isTls());
props.put("mail.smtp.host", this.smtpServer.getHostaddr());
props.put("mail.smtp.port", this.smtpServer.getPort());
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.debug", "true");
final String username = this.smtpServer.getUsername();
final String password = this.smtpServer.getPassword();
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
.
一种重现问题的一种方法:
我面临的问题是,如果我启动应用程序,例如,使用“更改邮件”功能,我将立即获取我的通知邮件。没有问题在那里。然后我会尝试购买一个产品,从而触发银行功能。
这是问题出现的位置:
Communication Error: javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException
- with linked exception:
[javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target]
.
另一种重现问题的方法:
现在让我们说我重新启动我的应用程序并尝试订购一些东西,这次它将工作,但邮件功能丢失了这个错误消息:
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
TRACE Error Could not connect to SMTP host: smtp.gmail.com, port: 465
.
底线:
如果我触发银行然后邮件 - >邮件不起作用 如果我触发邮件然后银行 - >银行不工作
任何可能在那里找到问题的人?
谢谢!
解决方案
您的“银行功能”正在改变信托商店。新的信任商店需要有必要的证书与邮件服务器验证SSL连接。您可以使用JDK默认信任存储库的所有CA证书初始化您的信任存储,或者您可以只添加邮件服务器的特定证书 - 请参阅 installcert
其他提示
问题是,如果没有信任库集,邮件功能正在运行(因为它使用位于:
中的系统的默认信任Strete/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/jre/lib/security/cacerts
.
在Mac上。
银行功能使用它是自己的证书,它位于:
MyProject/.../WEB-INF/sec/certs.jks
.
每次JavaMail试图对谷歌的SMTP服务器进行身份验证,它试图使用Certs.jks truststore,即使我取消了邮件方法中的Banking功能集的TrustStore属性。
修复:
在邮件的开头:
String path = FacesContext.getCurrentInstance().getExternalContext()
.getRealPath("/")
+ "WEB-INF/sec/certs.jks";
System.setProperty("javax.net.ssl.trustStore", path);
.
将默认的cacerts
密钥库导入我们自己的自定义密钥库:
keytool -importkeystore -srckeystore certs.jks -destkeystore cacerts
. 不隶属于 StackOverflow