浏览代码

fix ssh bug

Looly 6 年之前
父节点
当前提交
c7dcf6e6e9
共有 5 个文件被更改,包括 33 次插入22 次删除
  1. 1 0
      CHANGELOG.md
  2. 1 1
      README.md
  3. 29 19
      hutool-extra/src/main/java/cn/hutool/extra/ssh/JschUtil.java
  4. 1 1
      hutool-poi/pom.xml
  5. 1 1
      hutool-system/pom.xml

+ 1 - 0
CHANGELOG.md

@@ -7,6 +7,7 @@
 
 ### 新特性
 ### Bug修复
+* 【extra】      修复遗留的getSession端口判断错误(issue#594@Github)
 
 -------------------------------------------------------------------------------------------------------------
 

+ 1 - 1
README.md

@@ -51,7 +51,7 @@ Hutool是一个小而全的Java工具类库,通过静态方法封装,降低
 
 Hutool中的工具方法来自于每个用户的精雕细琢,它涵盖了Java开发底层代码中的方方面面,它既是大型项目开发中解决小问题的利器,也是小型项目中的效率担当;
 
-Hutool是项目中“util”包友好的替代,它节省了我们对项目中公用类和公用工具方法的封装时间,使开发专注于业务,同时可以最大限度的避免封装不完善带来的bug。
+Hutool是项目中“util”包友好的替代,它节省了开发人员对项目中公用类和公用工具方法的封装时间,使开发专注于业务,同时可以最大限度的避免封装不完善带来的bug。
 
 ### Hutool名称的由来
 

+ 29 - 19
hutool-extra/src/main/java/cn/hutool/extra/ssh/JschUtil.java

@@ -119,28 +119,13 @@ public class JschUtil {
 	 * @since 4.5.2
 	 */
 	public static Session createSession(String sshHost, int sshPort, String sshUser, String sshPass) {
-		Assert.notEmpty(sshHost, "SSH Host must be not empty!");
-		Assert.isTrue(sshPort < 0, "SSH Host must be not empty!");
-
-		// 默认root用户
-		if (StrUtil.isEmpty(sshUser)) {
-			sshUser = "root";
-		}
-
 		final JSch jsch = new JSch();
-		Session session;
-		try {
-			session = jsch.getSession(sshUser, sshHost, sshPort);
-		} catch (JSchException e) {
-			throw new JschRuntimeException(e);
-		}
+		final Session session = createSession(jsch, sshHost, sshPort, sshUser);
 
 		if (StrUtil.isNotEmpty(sshPass)) {
 			session.setPassword(sshPass);
 		}
 
-		// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
-		session.setConfig("StrictHostKeyChecking", "no");
 		return session;
 	}
 
@@ -156,19 +141,43 @@ public class JschUtil {
 	 * @since 5.0.0
 	 */
 	public static Session createSession(String sshHost, int sshPort, String sshUser, String privateKeyPath, byte[] passphrase) {
+		Assert.notEmpty(privateKeyPath, "PrivateKey Path must be not empty!");
+
+		final JSch jsch = new JSch();
+		try {
+			jsch.addIdentity(privateKeyPath, passphrase);
+		} catch (JSchException e) {
+			throw new JschRuntimeException(e);
+		}
+
+		return createSession(jsch, sshHost, sshPort, sshUser);
+	}
+
+	/**
+	 * 创建一个SSH会话,重用已经使用的会话
+	 *
+	 * @param jsch    {@link JSch}
+	 * @param sshHost 主机
+	 * @param sshPort 端口
+	 * @param sshUser 用户名,如果为null,默认root
+	 * @return {@link Session}
+	 * @since 5.0.3
+	 */
+	public static Session createSession(JSch jsch, String sshHost, int sshPort, String sshUser) {
 		Assert.notEmpty(sshHost, "SSH Host must be not empty!");
 		Assert.isTrue(sshPort > 0, "SSH port must be > 0");
-		Assert.notEmpty(privateKeyPath, "PrivateKey Path must be not empty!");
 
 		// 默认root用户
 		if (StrUtil.isEmpty(sshUser)) {
 			sshUser = "root";
 		}
 
-		final JSch jsch = new JSch();
+		if(null == jsch){
+			jsch = new JSch();
+		}
+
 		Session session;
 		try {
-			jsch.addIdentity(privateKeyPath, passphrase);
 			session = jsch.getSession(sshUser, sshHost, sshPort);
 		} catch (JSchException e) {
 			throw new JschRuntimeException(e);
@@ -176,6 +185,7 @@ public class JschUtil {
 
 		// 设置第一次登录的时候提示,可选值:(ask | yes | no)
 		session.setConfig("StrictHostKeyChecking", "no");
+
 		return session;
 	}
 

+ 1 - 1
hutool-poi/pom.xml

@@ -17,7 +17,7 @@
 
 	<properties>
 		<!-- versions -->
-		<poi.version>4.1.0</poi.version>
+		<poi.version>4.1.1</poi.version>
 		<xerces.version>2.12.0</xerces.version>
 	</properties>
 

+ 1 - 1
hutool-system/pom.xml

@@ -26,7 +26,7 @@
 		<dependency>
 			<groupId>com.github.oshi</groupId>
 			<artifactId>oshi-core</artifactId>
-			<version>4.0.0</version>
+			<version>4.1.0</version>
 			<scope>provided</scope>
 		</dependency>
 	</dependencies>