Java SSH 秘鑰連接mysql數(shù)據(jù)庫的方法
當(dāng)目標(biāo)數(shù)據(jù)庫不能直連的,需要一個服務(wù)器作為中間跳板的時候,我們需要通過SSH通道連接數(shù)據(jù)庫。
ps:使用ssh連接,相當(dāng)于本地開了個端口去連接遠(yuǎn)程的服務(wù),就是ssh通道,本地起的項(xiàng)目監(jiān)聽本地的端口,就可以使用這個通道進(jìn)行數(shù)據(jù)傳輸。
1、引入依賴<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency>2、代碼編寫
#ssh連接是否開啟ssh.forward.enabled=true#SSH連接跳板機(jī)地址 必填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#需要監(jiān)聽的遠(yuǎn)程服務(wù)的ip 必填ssh.forward.to_host=#需要監(jiān)聽的遠(yuǎn)程端口 必填ssh.forward.to_port=3306#SSH連接秘鑰地址,也可以使用rsa.ppk文件ssh.identity=C:Users69425.sshid_rsa2.1、配置 application.ssh.properties文件
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轉(zhuǎn)發(fā) 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數(shù)據(jù)庫連接用戶名 username: # mysql數(shù)據(jù)庫連接密碼 password: driver-class-name: com.mysql.cj.jdbc.Driver# 使用了druid去配置及監(jiān)控連接池和本文無關(guān),可加可不加 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、啟動項(xiàng)目
當(dāng)看到這里**“已使用ssh連接”**就可以了是連接成功了。
4、出現(xiàn)異常報錯com.jcraft.jsch.JSchException: invalid privatekey: [B@53a7a60c
這是秘鑰問題,這個異常只在使用秘鑰時候才會有。是秘鑰解析失敗,并不是使用秘鑰連接失敗。如果出現(xiàn)這個異??梢缘竭@篇文章中查看:詳解Java使用Jsch與sftp服務(wù)器實(shí)現(xiàn)ssh免密登錄。
這個依賴已經(jīng)很久沒更新了。但是目前本人未發(fā)現(xiàn)更好的ssh連接jar包 ↩︎
到此這篇關(guān)于Java SSH 秘鑰連接mysql數(shù)據(jù)庫的方法的文章就介紹到這了,更多相關(guān)Java ssh連接mysql數(shù)據(jù)庫內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python 如何在 Matplotlib 中繪制垂直線2. ASP常用日期格式化函數(shù) FormatDate()3. 開發(fā)效率翻倍的Web API使用技巧4. 如何通過python實(shí)現(xiàn)IOU計(jì)算代碼實(shí)例5. bootstrap select2 動態(tài)從后臺Ajax動態(tài)獲取數(shù)據(jù)的代碼6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動輸入功能實(shí)現(xiàn)代碼8. Python 操作 MySQL數(shù)據(jù)庫9. vue使用moment如何將時間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時間格式10. python中@contextmanager實(shí)例用法
