Detailed explanation of the architecture and component of Apache Mina SSHD :: SFTP
Apache Mina SSHD is a Java -based open source project for implementing the SSH server.It provides a safe and efficient file transmission protocol, that is, the SSH file transmission protocol (SFTP).In this article, we will introduce the SFTP architecture and components of Apache Mina SSHD in detail, and provide the necessary Java code examples.
SFTP architecture overview:
Apache Mina SSHD's SFTP architecture consists of the following core components:
1. SSHD: This component is the entrance point of the entire SFTP architecture.It is responsible for handling the client's SSH connection request and assigning the request to the corresponding processing program.SSHD is also responsible for the authentication process and security.
2. SFTPSUBSYSTEM: This is a subsystem of SSHD, responsible for handling all details of the SFTP protocol.It manages the user's SFTP session, handle requests and responses, and maintains the SFTP file system.
3. SFTPEventListener: This is an optional component for monitoring and processing SFTP events, such as file uploading, downloading, deleting.It can be implemented by implementing the SFTPEVENTLISTENER interface and registering it to the SFTP server.
SFTP component detailed explanation:
Now we will introduce the functions and use of each component more in detail.
1. SSHD:
SSHD is responsible for monitoring connection requests from the SFTP client and realizes authentication and security.The following is a simple example code to show how to use Apache Mina SSHD to create an SSHD instance:
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(22);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystem.Factory()));
sshd.setPasswordAuthenticator((username, password, session) -> "password".equals(password));
sshd.start();
In this example, we are equipped with the port number, key and subsystem factories of the SSH server.A password verification device is also set to verify the password provided by the client.Finally, start the SSH server by calling the `Start ()" method.
2. Sftpsubsystem :
SFTPSubsystem is one of the core components of Apache Mina SSHD.It is responsible for handling the details of the SFTP protocol, including user session management, requests and response processing, and the management of SFTP file systems.The following is an example code that shows how to add sftpsubsystem to SSHD instance:
sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystem.Factory()));
By adding SFTPSubsystem.Factory instances to the SSHD subsystem factory list, the SFTP function can be enabled.
3. SFTPEVENTLYSTER :
SFTPEVENTLISTENER is an optional component for monitoring and processing SFTP events.For example, you can create a listener to record events uploaded by each file.The following is an example code to show how to achieve the SFTPEVENTLISTENER interface and register it to the SFTP server:
public class MySftpEventListener implements SftpEventListener {
@Override
public void uploaded(SftpSession session, String remotePath, Path localPath, boolean isDirectory, CopyOption... copyOptions) {
System.out.println("File uploaded - " + remotePath);
}
}
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(22);
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser")));
sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystem.Factory()));
sshd.addSftpEventListener(new MySftpEventListener());
sshd.setPasswordAuthenticator((username, password, session) -> "password".equals(password));
sshd.start();
In this example, we created a custom monitor called MySFTPEVENTLISTENER, and implemented the `UPLOADED () method to process file upload events.Then add it to the SSHD instance by calling the method by calling the `addsftListener () method.
Summarize:
This article details the SFTP architecture and components of Apache Mina SSHD.Monitor and handle client connection requests by using SSHD components, and add sftpsubsystem to implement SFTP function.In addition, you can use SFTPEVENTLISTENER to monitor and handle the SFTP incident.The example code provided above can help you start using Apache Mina SSHD to achieve a safe and efficient SFTP server.