关于MySql的SSL-Connection

原因

之前在写一些有关增删改查的项目中,都是用的postgresql数据库,用到的服务器是wildfly,用的直接是用bin/jboss-cli.sh这个来配置的数据源,没有仔细研究一下究竟用jboss-cli.sh配置的数据源到底进行了那些修改.在最近换成用MySQL之后,出现了这么一个问题:
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.
这是我从Wildlfy终端日志中粘出来的,其实这个只是一个警告,不是错误.具体的意思是,说要你建立ssl链接,但是服务器本身没有身份认证,这种方式不推荐使用.但是你在链接的时候是要说明的,如果没有显示声明不适用ssl链接,它默认是使用的,就会导致如上警告.

具体操作

其实之前在用hibernate/jpa,甚至说是用jdbc的时候,仔细看一下你jdbc的connection url里面,其实是有这么一段的:

Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false","root", "letmein");

在jpa的persistence.xml文件中也有:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
  <persistence-unit name="defaultPersistenceUnit"  transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
      <!-- 使用MySQL方言 -->
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
      <!-- 数据库连接的URL地址 -->
      <property name="hibernate.connection.url"
        value="jdbc:mysql://localhost:3306/spring"/>
      <!-- 数据库连接的驱动 -->
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
      <!-- 数据库连接的用户名 -->
      <property name="hibernate.connection.username" value="root"/>
      <!-- 数据库连接的密码 -->
      <property name="hibernate.connection.password" value="314"/>
      <!-- 显示SQL语句 -->
      <property name="hibernate.show_sql" value="true"/>

      <property name="hibernate.connection.useUnicode" value="true"/>
      <property name="hibernate.connection.characterEncoding" value="UTF-8"/>

      <!-- 在显示SQL语句时格式化语句 -->
      <property name="hibernate.format_sql" value="true"/>
      <property name="hibernate.use_sql_comments" value="false"/>
      <!-- 自动输出schema创建DDL语句 -->
      <property name="hibernate.hbm2ddl.auto" value="create"/>

      <!-- 数据库连接超时后自动重连 -->
      <property name="hibernate.connection.autoReconnect" value="true"/>
      <property name="connection.autoReconnectForPools" value="true"/>
      <property name="connection.is-connection-validation-required" value="true"/>
    </properties>
  </persistence-unit>
</persistence>

例如,当使用到wildlfy数据源的时候,需要配置的就不是这里了,而是在standalone.xml中:
ps:在wildlfy中配置数据源的方式:
这里写链接内容
以下是使用mysql数据源时需要的配置:

<datasource jndi-name="java:jboss/datasources/mysqlDS" pool-name="mysqlDSPool">
    <connection-url>jdbc:mysql://localhost:3306/ejos?useSSL=false</connection-url>
    <driver>mysql</driver>
        <security>
            <user-name>root</user-name>
            <password>314</password>
        </security>
    <validation>  
        <valid-connection-checker class-name="org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker"/>  
        <validate-on-match>true</validate-on-match>  
        <background-validation>false</background-validation>  
        <background-validation-millis>60000</background-validation-millis>  
    <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>  
    </validation>
</datasource>

上面的connection-url需要加上对ssl的限定,不使用,下面是对链接的验证.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
环境: Eclipse Java EE IDE for Web Developers. Version: Oxygen.3a Release (4.7.3a) mysql-connector-java-8.0.11 MySQL Server 8.0 Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. 驱动的类换了个名字 Class.forName("com.mysql.cj.jdbc.Driver"); Fri Jun 08 13:09:27 CST 2018 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. 加上useSSL=true或false 参考地址 con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useSSL=true", "root", "123456"); java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support. 加上serverTimezone=GMT 参考地址 con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useSSL=true&serverTimezone=GMT", "root", "123456")

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值