繁体   English   中英

具有SSL授权证书的Java MySQL连接

[英]Java MySQL Connection with SSL Authority Certificate

我正在尝试使用Java Rest WebService连接到JDBC

首先,我使用用户名和密码以及一个额外的细节使用MySQL Workbench成功连接,我需要添加“用于SSL的证书颁发机构文件的路径”并工作,我能够连接和使用。我具有使用远程数据库的证书。

现在,我尝试使用Java进行连接,而这就是我尝试的方式:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConexaoBD {
    // Define o Metodo de Conexao quando a classe � chamada
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public Connection obtemConexao() throws SQLException {
        System.getProperties().setProperty("javax.net.ssl.trustStore","/mysqlcertificate.pem");
        return DriverManager.getConnection("jdbc:mysql://myHost", "myUsername", "myPassword");                                  
    }
}

我研究了URL中的useuse == true,但没有起作用,或者这个system.getproperties ,我还在项目包和类包中添加了.pem文件(因为我不知道我需要的位置是什么放置文件)。

我应该怎么做才能连接? 如果没有此SSL,数据库将拒绝我的连接,而我试图以这种方式连接另一个类:

Connection conn = null;
Statement stm = null;
try {
    ConexaoBD bd = new ConexaoBD();
    conn = bd.obtemConexao();

谁能帮我?

我的情况是相似的,但不相同。 我可以从工作台连接到MySQL,但不能从我的代码连接到MySQL。 如果可用,在工作台中选择的唯一选项是useSSL。 在这种情况下,以下代码对我有用。

private String db_server = BaseMethods.getSystemData("db_server");
private String db_user = BaseMethods.getSystemData("db_user");
private String db_password = BaseMethods.getSystemData("db_password");

private void connectToDb() throws Exception {
    String jdbcDriver = "com.mysql.jdbc.Driver";
    String dbUrl = "jdbc:mysql://" + db_server +
        "?verifyServerCertificate=false" +
        "&useSSL=true" +
        "&requireSSL=true";
    System.setProperty(jdbcDriver, "");
    Class.forName(jdbcDriver).newInstance();

    Connection conn = DriverManager.getConnection(dbUrl, db_user, db_password);
    Statement statement = conn.createStatement();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM