【问题标题】:Spring Boot remote Mysql Connection through SSL通过 SSL 的 Spring Boot 远程 Mysql 连接
【发布时间】:2018-07-02 00:52:39
【问题描述】:

我有一个远程 MySQL 数据库服务器,需要与我在本地运行的 Spring Boot 应用程序连接。

在加载 Spring Boot 应用程序之前,它会从与远程服务器的 ssh 隧道开始。

这是首先创建和 ssh 隧道然后启动主应用程序的主应用程序代码。

public class XXXCoreApplication {

public static void main(String[] args) throws SQLException {
    Connection connection = null;
    Session session = null;

    String host = "XX.XXX.XX.XXX";
    String servUser = "user";
    String servPwd = "pass";
    int port = 22;

    String rhost = "localhost";
    int rport = 3306;
    int lport = 3307;

    String driverName = "com.mysql.jdbc.Driver";
    String db2Url = "jdbc:mysql://localhost:" + lport + "/xxx_core";
    String dbUsr = "MT";
    String dbPwd = "****";

    try {
        JSch jsch = new JSch();
        // Get SSH session
        session = jsch.getSession(servUser, host, port);
        session.setPassword(servPwd);
        java.util.Properties config = new java.util.Properties();
        // Never automatically add new host keys to the host file
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        // Connect to remote server
        session.connect();
        // Apply the port forwarding
        session.setPortForwardingL(lport, rhost, rport);
        // Connect to remote database
        Class.forName(driverName);
        connection = DriverManager.getConnection(db2Url, dbUsr, dbPwd);
        System.out.println("Connection to database established!");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null && !connection.isClosed()) {
            connection.close();
        }
        if (session != null && session.isConnected()) {
            session.disconnect();
        }
    }
    SpringApplication.run(XXXCoreApplication.class, args);
}

}

但是我的 application.properties 文件是空的,可能是故障点。

它给了我一个空 application.properties 的错误:

"无法确定数据库类型的嵌入式数据库驱动程序类 无”

我应该为 application.properties 中的 spring.datasource.url 值赋予什么?或者有什么其他推荐吗?谢谢。

【问题讨论】:

  • db2Url使用的一样...
  • @M.Deinum 不幸的是它不起作用。
  • 您显然还需要提供其他属性(用户名等)。同样在您的代码中,您在建立连接后关闭会话。所以你基本上是在启动应用程序之前关闭隧道。
  • 哦,指点隧道关闭问题解决了!您可以回复作为答案而不是评论给更多读者吗?非常感谢@M.Deinum

标签: mysql spring spring-mvc spring-boot ssh


【解决方案1】:

您正在关闭 finally 块中的 SSH 隧道。

我建议您也让 Spring 创建 SSH 隧道,而不是在您的 main 方法中创建。这将允许您选择性地启用/禁用不同环境的隧道,并且您可以使用属性和其他 Spring Boot 功能来配置、启动和停止隧道。

这样的事情会奏效。

public class SshTunnelStarter {

    @Value("${ssh.tunnel.url}")
    private String url;

    @Value("${ssh.tunnel.username}")
    private String username;

    @Value("${ssh.tunnel.password}")
    private String password;

    @Value("${ssh.tunnel.port:22}")
    private int port;

    private Session session;

    @PostConstruct
    public void init() throws Exception {
       JSch jsch = new JSch();
       // Get SSH session
       session = jsch.getSession(servUser, host, port);
       session.setPassword(servPwd);
       java.util.Properties config = new java.util.Properties();
       // Never automatically add new host keys to the host file
       config.put("StrictHostKeyChecking", "no");
       session.setConfig(config);
       // Connect to remote server
       session.connect();
       // Apply the port forwarding
       session.setPortForwardingL(lport, rhost, rport);
    }

    @PreDestroy
    public void shutdown() throws Exception {
       if (session != null && session.isConnected()) {
           session.disconnect();
       }
    }
}

这样您就可以使用 Spring 来配置和管理隧道。如果你愿意,你甚至可以让它对某些环境有条件。进行本地开发时没有隧道,并为测试和生产环境启用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 2018-11-23
    • 2020-11-19
    • 2018-12-05
    • 2011-05-16
    • 1970-01-01
    • 2015-11-29
    相关资源
    最近更新 更多