Apache mina sshd :: sFTP's advanced configuration and parameter description
Apache Mina SSHD is a library for SSH servers and clients for Java.It provides a safe file transmission protocol (SFTP).This article will introduce the advanced configuration and parameter description of the Apache Mina SSHD, and with some Java code examples to help readers better understand.
1. Add SSHD dependencies
To use Apache Mina SSHD, you first need to add corresponding dependencies to the project construction file.The following is an example of using Maven to build a project:
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>2.7.0</version>
</dependency>
2. Create the SFTP server
To create a SFTP server, we need to define a class that inherits from `sftpsubsystemFactory`, and rewrite the method of` createSubsystem ().In this method, we can set some advanced configuration parameters.The following is an example:
import org.apache.sshd.common.Factory;
import org.apache.sshd.server.command.Command;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
public class CustomSftpSubsystemFactory extends SftpSubsystemFactory {
@Override
public Command createSubsystem(Session session) {
// Set the advanced configuration parameters of the SFTP server
// You can set the file of SFTP here to open the limit, file upload/download speed, etc.
// For example, set the maximum number of files to open is 100
session.setAttribute(SshConstants.MAX_OPEN_FILES, 100);
return super.createSubsystem(session);
}
}
3. Start the SSH server
To start a SSH server and use the SFTP server we define, we need to create an object of a `SSHSERVER` and set some configurations.The following is an example:
import org.apache.sshd.common.util.security.SecurityUtils;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import java.io.IOException;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
public class SftpServerExample {
public static void main(String[] args) throws IOException, GeneralSecurityException {
// Initialize the security module of Apache Mina SSHD
SecurityUtils.setRegisterBouncyCastle(true);
// Create the SSH server
SshServer sshd = SshServer.setUpDefaultServer();
// Set the advanced configuration parameters of the SSH server
// You can set the port of SSH here, host key, subsystem factory, etc.
// Set the monitor port of the SSH server as 2222
sshd.setPort(2222);
// Set the storage path of the server key
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("hostkey.ser")));
// Set the SFTP server
sshd.setSubsystemFactories(Collections.singletonList(new CustomSftpSubsystemFactory()));
// Start the SSH server
sshd.start();
// Waiting for the program to terminate, for example, turn off the server through user input
System.out.println("Press any key to stop the server...");
System.in.read();
// Close the SSH server
sshd.stop();
}
}
In the above example, we set up the SSH server's monitoring port to 2222 by calling the method of calling the `setport ()` method, and use the `setKeypairprovider ()` method to set the storage path of the server key.Then, we add the custom SFTP server to the SSH server through the method of `setsubsystemFactories ()` ``).
We can also set the senior configuration parameters of the SFTP server in the sftp server in the sftp server through the method of `session.settattribute ()` method.For example, we can set the maximum number of files to allow the SFTP server to open.
This is a knowledge article about the high -end SFTP configuration and parameter description of Apache Mina SSHD.By using these configurations and parameters, you can better control and optimize the behavior and performance of the SFTP server.Hope this article will help you!