增强 SSH/SFTP 稳定性并完善安全校验与前端交互

This commit is contained in:
liumangmang
2026-03-11 23:14:39 +08:00
parent 8845847ce2
commit 085123697e
34 changed files with 1433 additions and 605 deletions

View File

@@ -1,10 +1,10 @@
package com.sshmanager.service;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.sshmanager.entity.Connection;
import org.springframework.stereotype.Service;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.sshmanager.entity.Connection;
import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;
import java.io.InputStream;
@@ -13,10 +13,10 @@ import java.io.PipedInputStream;
import java.io.PipedOutputStream;
@Service
public class SshService {
public SshSession createShellSession(Connection conn, String password, String privateKey, String passphrase)
throws Exception {
public class SshService {
public SshSession createShellSession(Connection conn, String password, String privateKey, String passphrase)
throws Exception {
JSch jsch = new JSch();
if (conn.getAuthType() == Connection.AuthType.PRIVATE_KEY && privateKey != null && !privateKey.isEmpty()) {
@@ -31,8 +31,14 @@ public class SshService {
// Use only DH-based kex to avoid "Algorithm ECDH not available" on Java 8 / minimal JRE
session.setConfig("kex", "diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1");
if (conn.getAuthType() == Connection.AuthType.PASSWORD && password != null) {
if (conn.getAuthType() == Connection.AuthType.PASSWORD) {
if (password == null || password.isEmpty()) {
throw new IllegalArgumentException("Password is required for password authentication");
}
session.setConfig("PreferredAuthentications", "password");
session.setPassword(password);
} else {
session.setConfig("PreferredAuthentications", "publickey");
}
session.connect(10000);
@@ -59,10 +65,10 @@ public class SshService {
}
}).start();
return new SshSession(session, channel, channelOut, pipeToChannel);
}
return new SshSession(session, channel, channelOut, pipeToChannel);
}
public static class SshSession {
public static class SshSession {
private final Session session;
private final ChannelShell channel;
private final InputStream outputStream;
@@ -95,5 +101,5 @@ public class SshService {
public boolean isConnected() {
return channel != null && channel.isConnected();
}
}
}
}
}