进行需要连接数据库的操作时,控制台会报下面这个警告,看着不舒服,所以可以解决一下,你可以直接看英文,也能够看得懂。

Sun May 09 21:49:05 GMT+08:00 2021 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

翻译:

不建议在没有服务器身份验证的情况下建立SSL连接,根据MySQL 5.5.45+、5.6.26+和5.7.6+的要求,如果没有设置显式选项,则必须默认建立SSL连接。为了符合不使用SSL的现有应用程序,verifyServerCertificate属性被设置为“false”。您需要通过设置useSSL=false来显式禁用SSL,或者通过设置useSSL=true来为服务器证书验证提供信任存储

简单的来说, 就是需要设置一下useSSL,你可以设置为false来禁用SSL,或者设置为true来使用SSL,报这个警告的原因主要是JDBC的版本与MySQL的版本不兼容,而MySQL在高版本需要指明是否进行SSL连接。

辅助阅读:

   SSL协议提供服务主要: 		
       1)认证用户服务器,确保数据发送到正确的服务器;    .
       2)加密数据,防止数据传输途中被窃取使用;
       3)维护数据完整性,验证数据在传输过程中是否丢失;

   当前支持SSL协议两层:
   	 	SSL记录协议(SSL Record Protocol):建立靠传输协议(TCP)高层协议提供数据封装、压缩、加密等基本功能支持
	    SSL握手协议(SSL Handshake Protocol):建立SSL记录协议用于实际数据传输始前通讯双进行身份认证、协商加密
	    算法、 交换加密密钥等。

解决办法:

在MySQL连接字符串的url中加上配置即可,比如:
spring.datasource.url=jdbc:mysql://localhost:3306/shaonaiyi?characterEncoding=UTF8&autoReconnect=true&useSSL=true

注意:低版本的MySQL使用useSSL=true可能会报错,使用useSSL=false不会报错,可以自行找资料解决。

报错样子如下:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors
Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors

作用:

当配置MySQL端口为SSL,数据通道会加密处理,这样可以避免敏感信息泄漏和被篡改。
但是,启用MySQL的SSL之后,因为每个数据包都需要加密和解密,所以会对MySQL的性能产生不小的影响,大家在使用的时候,可以根据实际情况看是否要开启。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐