SoFunction
Updated on 2025-03-06

How to connect to a remote server via SSH in springboot

Business scenarios

In terms of security considerations and the server is not in the same LAN, the access address of the server is not suitable to be placed directly on the public network, and the local server needs to forward it to the remote server through the ssh channel.

Springboot single project connects to the server.

Solution

Introduce dependencies

		<dependency>
            <groupId></groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.53</version>
        </dependency>

Account password verification connection SSH

  • SSH connection tool class
import ;
import ;
import ;

/**
  * SSH connection remote server forwarding tool class
  */
public class SshConnectionTool {

    //Ssh connection username    private final static String SSH_USER = "test";
    //Ssh connection password    private final static String SSH_PASSWORD = "123456";
    //The IP address of the remote connection of ssh    private final static String SSH_REMOTE_SERVER = "12.34.56.78";
    //The port number of ssh connection    private final static int SSH_REMOTE_PORT = 2721;
    //The IP address of the remote mysql connection    private final static String MYSQL_REMOTE_SERVER = "124.543.789.111";
    //The port number used when connecting to the local database is the same as the port configured by yml    private final static int LOCAl_PORT = 3309;
    //The port number used for the remote database port    private final static int REMOTE_PORT = 31494;

    private Session sesion; //ssh session
    public void closeSSH ()
    {
        ();
    }

    public SshConnectionTool () throws Throwable
    {

        JSch jsch = new JSch();

        sesion = (SSH_USER, SSH_REMOTE_SERVER, SSH_REMOTE_PORT);

        (SSH_PASSWORD);

        //Set the connection process not to verify the information in the known_hosts file        Properties config = new Properties();
        ("StrictHostKeyChecking", "no");
        (config);

        (); //ssh establish a connection!
        // According to security policy, you must connect via the forwarding port        (LOCAl_PORT, MYSQL_REMOTE_SERVER, REMOTE_PORT);
    }
}
  • SSH connection monitor
import ;
import ;
import ;

/**
  * SSH link Monitor
  */
@Component
public class SshContextListener implements ServletContextListener {
    private SshConnectionTool conexionssh;
    public SshContextListener() {
        super();
    }
    /**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent arg0) {
        ("Context initialized ... !");
        try {
            conexionssh = new SshConnectionTool();
        } catch (Throwable e) {
            (); // Connection failed        }
    }

    /**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent arg0) {
        ("Context destroyed ... !");
        (); // Disconnect    }
}
  • yml connection pool configuration
url: jdbc:mysql://127.0.0.1:3309/test?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: root
type: 

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.