February 23, 2019

In this blog post, we are going to see how to setup a SFTP Server using Java and Spring boot. We will be using a library/framework called as Apache MINA which provide us with the possibility to create different kinds of network applications, in our case as we want to create a SFTP server, we will be using the library Apache SSHD . This library provides us a way to support SSH protocols in our application.
Also we will be using Spring Boot with Maven to demonstrate the project.

Server Setup

Setting up the SFTP server is pretty simple: first, we have to add the following dependencies to our project:

<dependency>
   <groupId>org.apache.mina</groupId>
   <artifactId>mina-core</artifactId>
   <version>1.1.7</version>
</dependency>
<dependency>
   <groupId>org.apache.sshd</groupId>
   <artifactId>sshd-core</artifactId>
   <version>1.7.0</version>
</dependency>

Once we have added these dependencies, we create a new class called as MySftpServer.java and add a method called as start() , this method contains the logic to spin up a SSH server.

package com.programming.techie.javasftpserver;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.Collections;

@Service
public class MySftpServer {

    private Log log = LogFactory.getLog(MySftpServer.class);

    @PostConstruct
    public void startServer() throws IOException {
        start();
    }

    private void start() throws IOException {
        SshServer sshd = SshServer.setUpDefaultServer();
        sshd.setPort(2222);
        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("host.ser")));
        sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
        sshd.setPasswordAuthenticator((username, password, session) -> username.equals("test") && password.equals("password"));
        sshd.start();
        log.info("SFTP server started");
    }
}

In the line 28, we are creating a host private key in a file called as host.ser . If you are creating a fully functional SFTP interface you should usually store this file in a secure location, where it can only be accessed by the administrator.

NOTE: Once you have created the server and started accepting connections from the client, make sure that you do not loose/destory the host.ser file, this file is used to authenticate by the client to make sure that it is connecting to the right host or not.

In line 29, we create a SftpSubsystemFactory, to add SFTP capabilities to our SSH server.

In line 30, we create a simple username and password based authentication mechanism, in our case all the incoming connections should provide username and password while starting the SFTP session. Here we are using a hardcoded username and password for demonstration, in the next secion we will see how to enable Public Key Authentication for our SFTP server.

Line 31, starts our SFTP server followed by a simple log statement to tell us that the server is started.

As this example is based on Spring Boot, this is how our main class looks like:

package com.programming.techie.javasftpserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JavaSftpserverApplication {

 public static void main(String[] args) {
  SpringApplication.run(JavaSftpserverApplication.class, args);
  while (true);
 }

}

Note that we added a statement while(true) to keep running the application, in this way it continues to run and listen to the incoming connections. So we just created a very minimal but functional SFTP server, lets test this.

To start an sftp session, I am going to use git bash, as I am using a Windows machine. Here is the command I am going to use:

Once you type the above command it asks whether to trust the connection or not, type yes and then enter. Now it will ask for the password, type the password we have provided above (“password”), now it should successfully start our SFTP session. You can now upload files to the server.

The code for this tutorial is hosted in Github.
In the next blog posts, we will have a look how to setup Public Key Authentication / Password less authentication for this setup.

Part 2 – SFTP Server with Public Key Authentication

About the author 

Sai Upadhyayula

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Subscribe now to get the latest updates!