javax.mail-强制IMAP使用TLS1.2 +-“收到的致命警报:协议版本”

问题描述 投票:0回答:2

我的应用程序在通过IMAP读取邮件时遇到问题。

应用程序读取多个imap帐户。应用程序可以连接到除需要使用TLS1.2或TLS1.3的服务器之外的所有服务器。

我有此代码:

System.out.println("SSLContext getProtocols(): " + String.join(" ", SSLContext.getDefault().getSupportedSSLParameters().getProtocols()));

Properties props = new Properties();
props.put("mail.imap.auth", "true");        
props.put("mail.imap.ssl.enable", "true");          
props.put("mail.imap.starttls.enable", "false");
props.put("jdk.tls.client.protocols", "TLSv1.2 TLSv1.3");
props.put("mail.imap.ssl.protocols", "TLSv1.2 TLSv1.3");

Session session = Session.getDefaultInstance(props);
Store store = session.getStore("imaps");
store.connect(mailConf.getImapServer(), mailConf.getImapServerPort(), mailConf.getImapUser(), mailConf.getImapPass());
Folder inbox = store.getFolder(mailConf.getImapMailFolder());
inbox.open(Folder.READ_ONLY);

并使用以下命令运行应用程序:

java -Djdk.tls.client.protocols="TLSv1.2,TLSv1.3" \
-Ddeployment.security.SSLv2Hello=false \
-Ddeployment.security.SSLv3=false \
-Ddeployment.security.TLSv1=false \
-Ddeployment.security.TLSv1.1=false \
-Ddeployment.security.TLSv1.2=true \
-Ddeployment.security.TLSv1.3=true \
-jar myApp.jar

对于需要TLS1.2 +的IMAP服务器,我会看到以下输出:

SSLContext getProtocols(): TLSv1.3 TLSv1.2 TLSv1.1 TLSv1 SSLv3 SSLv2Hello
Jan 29 11:41:19 myhost myapp[22101]: 2020-01-29 11:41:19 ERROR e.v.MyAppApplication$$EnhancerBySpringCGLIB$$8bb5321c:68 - Imap readMail() error: Received fatal alert: protocol_version

我正在使用Java 11:

# java --version
openjdk 11.0.4 2019-07-16
OpenJDK Runtime Environment (build 11.0.4+11-post-Debian-1deb10u1)
OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Debian-1deb10u1, mixed mode, sharing)

我做的不好吗?如何强制TLS1.2 +用于imap连接?

谢谢。

java ssl javamail imap sslcontext
2个回答
0
投票

您可以从JavaMail属性中删除jdk.tls.client.protocols属性,因为JavaMail绝对不会解释该属性。在命令行上将其设置为System属性是可以的,因为JDK会对其进行解释。

我没有看到有关deployment.security.*属性的任何文档,但是我看到的建议仅在您使用的不是Java插件或Java WebStart时才有意义。因此,您可能不需要设置这些属性。

您收到的错误消息表明您的应用程序正在使用Spring,尽管在您发布的JavaMail代码中没有看到它。也许Spring会以某种方式插入套接字设置代码并控制安全性配置?

您可以尝试使用独立的JavaMail msgshow.java示例程序来证明您可以使用该JDK版本建立连接,而无需进行特殊的安全配置。如果失败,则问题出在JDK安全配置中。


0
投票

我找到解决方法:将Spring Boot从2.1.1.RELEASE升级到2.2.4.RELEASE

© www.soinside.com 2019 - 2024. All rights reserved.