在测试服务器上,java程序启动的时候,日志里面出现javax.net.ssl.SSLHandshakeException,这个错误目前尚未发现是什么缘由致使的,大几率是有人升级了mysql或者jdk的版本。 在查找多方资料发现,是jdk8和mysql支持的协议不一致致使的。
mysql版本:5.7.34
jdk版本:1.8.0_292html
1.jdk8从小版本JDK8u261开始支持TLS1.3,默认TLS1.2以前的协议在security里面是禁用的,因此从JDK8u261开始只有TLS1.2和1.3是默认启用的。jdk jsse连接java
The JSSE API supports the following security protocols:
TLS: version 1.0, 1.1, 1.2, and 1.3 (since JDK 8u261)
SSL (Secure Socket Layer): version 3.0
复制代码
2.mysql对于ssl的支持,咱们看下网上的描述:mysql
TLS versions: The allowable versions of TLS protocol can be restricted using the connection properties enabledTLSProtocols and, for X DevAPI connections and for release 8.0.19 and later, xdevapi.tls-versions (when xdevapi.tls-versions is not specified, it takes up the value of enabledTLSProtocols). If no such restrictions have been specified, Connector/J attempts to connect to the server with the following TLS versions:
TLSv1,TLSv1.1,TLSv1.2,TLSv1.3 for MySQL Community Servers 8.0, 5.7.28 and later, and 5.6.46 and later, and for all commercial versions of MySQL Servers.
TLSv1,TLSv1.1 for all other versions of MySQL Servers.
Notes
For Connector/J 8.0.26 and later: The TLSv1 and TLSv1.1 protocols have been deprecated. While connections to the server using those TLS versions can still be made with the same negotiation process as described above, for any connections established using those TLS versions, Connector/J writes to its logger the message "This connection is using TLSv1[.1] which is now deprecated and will be removed in a future release of Connector/J."
For Connector/J 8.0.18 and earlier when connecting to MySQL Community Server 5.6 and 5.7 using the JDBC API: Due to compatibility issues with MySQL Server compiled with yaSSL, Connector/J does not enable connections with TLSv1.2 and higher by default. When connecting to servers that restrict connections to use those higher TLS versions, enable them explicitly by setting the Connector/J connection property enabledTLSProtocols (e.g., set enabledTLSProtocols=TLSv1,TLSv1.1,TLSv1.2).
复制代码
这里很清楚的说明了,若是没有指定协议的状况下,在8.0.26以及以后的版本,TLS1.0和TLS1.1已经被废弃,可是能够做为握手的协议,会在mysql的日志记录器中提示这些协议会在将来被删除。 对于8.0.18以及以前的版本,包括5.6,5.7来讲,为了兼容性的缘由,不会启用1.2以及更高版本的TLS协议,因此mysql5.7这里默认是TLS1.0,TLS1.1被使用。 而jdk这边默认使用TLS1.2,TLS1.3。因此协议彻底对不上,就在握手的时候报了SSLHandshakeException。spring
如下有三种解决方式:
第一种暴力的把jdk默认禁用安全协议给去掉了(或者只去掉TLS1.0和TLS1.1也能够),不太推荐使用,JDK默认的协议你给它直接暴力注释,总归是会带来一些安全问题。
第二种方式也很蛮力直接把ssl给去掉了,双方直接不使用ssl,也不太推荐使用,理由同上
第三种方式直接指定使用某种双方都支持的协议,mysql说了为了兼容问题不主动使用TLS1.2协议,这里确认没有兼容性问题,直接指定协议便可。sql
在服务器输入命令:
which is java
返回:
/usr/bin/java
输入:ls -l /usr/bin/java
返回:/usr/bin/java -> /etc/alternatives/java
输入:ls -l /etc/alternatives/java
返回:/etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
而后知道java的目录以后,一步步进入到/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security
cd /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security
编辑java.sercurity
sudo vim java.security
而后搜索 SSLv3,找到 jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, RC4, DES, MD5withRSA, 相似的行
而后把这几行给注释掉
# Example:
# jdk.tls.disabledAlgorithms=MD5, SSLv3, DSA, RSA keySize < 2048
# jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, RC4, DES, MD5withRSA, \
# DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
# include jdk.disabled.namedCurves
保存退出,而后重启java服务
复制代码
在链接数据库的地方设置不使用ssl,参数加上useSSL=false数据库
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/database?useSSL=false
复制代码
在链接后指定协议TLSv1.2vim
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/database?enabledTLSProtocols=TLSv1.2
复制代码
1.stackoverflow问题
2.mysql链接ssl
3.JSSEapi