您的位置:首页技术文章
文章详情页

Java SSH 秘钥连接mysql数据库的方法

【字号: 日期:2022-08-09 16:33:13浏览:21作者:猪猪

当目标数据库不能直连的,需要一个服务器作为中间跳板的时候,我们需要通过SSH通道连接数据库。

ps:使用ssh连接,相当于本地开了个端口去连接远程的服务,就是ssh通道,本地起的项目监听本地的端口,就可以使用这个通道进行数据传输。

1、引入依赖

<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency>2、代码编写

#ssh连接是否开启ssh.forward.enabled=true#SSH连接跳板机地址 必填ssh.forward.host=#SSH连接端口 必填 固定ssh.forward.port=22#SSH连接用户名 必填 ssh.forward.username=#SSH连接密码 ssh.forward.password=#本地起的 必填 固定ssh.forward.from_host=localhost#本地开启的端口 必填 ssh.forward.from_port=3307#需要监听的远程服务的ip 必填ssh.forward.to_host=#需要监听的远程端口 必填ssh.forward.to_port=3306#SSH连接秘钥地址,也可以使用rsa.ppk文件ssh.identity=C:Users69425.sshid_rsa2.1、配置 application.ssh.properties文件

Java SSH 秘钥连接mysql数据库的方法Java SSH 秘钥连接mysql数据库的方法Java SSH 秘钥连接mysql数据库的方法

2.2、配置 SshConfiguration代码

import com.jcraft.jsch.JSch;import com.jcraft.jsch.JSchException;import com.jcraft.jsch.Session;import lombok.extern.slf4j.Slf4j;import org.springframework.boot.web.servlet.ServletContextInitializer;import org.springframework.stereotype.Component;import javax.servlet.ServletContext;import javax.servlet.ServletException;import java.io.IOException;import java.util.Properties;@Slf4j@Componentpublic class SshConfiguration implements ServletContextInitializer { public SshConfiguration() {try { Properties p = new Properties(); p.load(getClass().getResourceAsStream('/application.ssh.properties')); //如果配置文件包含ssh.forward.enabled属性,则使用ssh转发 if (p.getProperty('ssh.forward.enabled') != null) {log.info('ssh forward is opend.');log.info('ssh init ……');JSch jSch = new JSch();//需要使用秘钥时添加jSch.addIdentity(p.getProperty('ssh.identity'));Session session = jSch.getSession(p.getProperty('ssh.forward.username'), p.getProperty('ssh.forward.host'), Integer.parseInt(p.getProperty('ssh.forward.port')));session.setConfig('StrictHostKeyChecking', 'no');session.setPassword(p.getProperty('ssh.forward.password'));session.connect();session.setPortForwardingL(p.getProperty('ssh.forward.from_host'), Integer.parseInt(p.getProperty('ssh.forward.from_port')), p.getProperty('ssh.forward.to_host'), Integer.parseInt(p.getProperty('ssh.forward.to_port'))); } else {log.info('ssh forward is closed.'); }} catch (IOException e) { log.error('ssh IOException failed.', e);} catch (JSchException e) { log.error('ssh JSchException failed.', e);} catch (Exception e) { log.error('ssh settings is failed. skip!', e);} } @Override public void onStartup(ServletContext servletContext) throws ServletException {log.info('已使用ssh连接'); }}2.3、application.yum

spring: datasource:# localhost:3307 这里是ssh.forward.from_host:ssh.forward.from_port url: jdbc:mysql://localhost:3307/mysql?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8# mysql数据库连接用户名 username: # mysql数据库连接密码 password: driver-class-name: com.mysql.cj.jdbc.Driver# 使用了druid去配置及监控连接池和本文无关,可加可不加 druid: initial-size: 2 min-idle: 1 max-active: 10 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 validation-query: select ’x’ test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: true3、启动项目

Java SSH 秘钥连接mysql数据库的方法

当看到这里**“已使用ssh连接”**就可以了是连接成功了。

4、出现异常报错

com.jcraft.jsch.JSchException: invalid privatekey: [B@53a7a60c

这是秘钥问题,这个异常只在使用秘钥时候才会有。是秘钥解析失败,并不是使用秘钥连接失败。如果出现这个异常可以到这篇文章中查看:详解Java使用Jsch与sftp服务器实现ssh免密登录。

这个依赖已经很久没更新了。但是目前本人未发现更好的ssh连接jar包 ↩︎

到此这篇关于Java SSH 秘钥连接mysql数据库的方法的文章就介绍到这了,更多相关Java ssh连接mysql数据库内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Java
相关文章: