キーストアを使用した後のJavaMailの問題
-
26-12-2019 - |
質問
私は現在取り組んでいるJSFアプリケーションに奇妙な問題があります。衝突している私のプログラムの2つの部分があるようです。
2つの部分があります。
- 「銀行」機能
- メール機能
銀行機能の関連部分(この演習のためだけの偽の銀行です):
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);
.
問題を再現する1つの方法:
私が直面している問題は、私がアプリケーションを起動した場合、 "Mail"機能を使用することで、すぐに通知メールを入手します。問題ありません。それから私は製品を購入しようとし、銀行の機能を引き起こします。
問題が表示される場所:
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 プログラム。最後に、既定のTrust Storeを上書きするのではなく、銀行機能を明示的に使用するように銀行機能を変更するようにJavaMailを設定することもできます。それらはおそらくより複雑です。
他のヒント
問題は、トラストストアセットがない場合はメール機能が機能していたことが問題でした(
にあるシステムのデフォルトのトラストストアを使用しているため)。/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/jre/lib/security/cacerts
.
Mac上の
銀行機能は、それが含まれていたそれ自身の証明書を使用しています:
MyProject/.../WEB-INF/sec/certs.jks
.
JavaMailがGoogleのSMTPサーバーに認証しようとしたたびに、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
.