Java 如何在 Apache Mina Sshd 服务器中设置根目录
How to Set Root Directory in Apache Mina Sshd Server in Java
我使用 Apache Mina Sshd API 在 java.In SFTP 客户端中启动本地 SFTP 服务器 我使用 Jcraft jsch API 创建我的 SFTP client.I 成功启动一个 server.The 问题是我想写一些单元测试用例来检查客户端是否可以将一些文件放入服务器的根目录.目前我的 SFTP 服务器没有任何根目录 directory.So 我想知道是否有任何方法可以设置服务器的根目录。
例如:C:\sftp 我如何将此路径设置为我的服务器根目录 directory.so 然后客户端每次连接时都可以读取和写入文件server.Thank你。
public class SftpServerStarter {
private SshServer sshd;
private final static Logger logger =
LoggerFactory.getLogger(SftpServerStarter.class);
public void start(){
sshd = SshServer.setUpDefaultServer();
sshd.setPort(22);
sshd.setHost("localhost");
sshd.setPasswordAuthenticator(new MyPasswordAuthenticator());
sshd.setPublickeyAuthenticator(new MyPublickeyAuthenticator());
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
sshd.setSubsystemFactories(
Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
sshd.setCommandFactory(new ScpCommandFactory());
try {
logger.info("Starting ...");
sshd.start();
logger.info("Started");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.info("Can not Start Server");
}
}
}
在默认情况下,它采用名为 user.dir
的系统 属性 的根路径
为了改变这一点,您可以覆盖 NativeFileSystemView
和 return 路径中的 getVirtualUserDir()
。
sshd.setFileSystemFactory(new NativeFileSystemFactory() {
@Override
public FileSystemView createFileSystemView(final Session session) {
return new NativeFileSystemView(session.getUsername(), false) {
@Override
public String getVirtualUserDir() {
return "C:\MyRoot";
}
};
};
});
您也可以按照以下link了解如何在不同sshd-core版本的Apache Mina sshd SFTP服务器中设置根目录。
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>0.10.0</version>
</dependency>
进入
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>0.14.0</version>
</dependency>
在较新的 sshd 版本中,您可以使用 org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory
并通过方法 setFileSystemFactory
.
将其提供给 SshServer
实例
片段:
VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory();
fileSystemFactory.setDefaultHomeDir("home.directory");
sshd.setFileSystemFactory(fileSystemFactory)
我使用 Apache Mina Sshd API 在 java.In SFTP 客户端中启动本地 SFTP 服务器 我使用 Jcraft jsch API 创建我的 SFTP client.I 成功启动一个 server.The 问题是我想写一些单元测试用例来检查客户端是否可以将一些文件放入服务器的根目录.目前我的 SFTP 服务器没有任何根目录 directory.So 我想知道是否有任何方法可以设置服务器的根目录。
例如:C:\sftp 我如何将此路径设置为我的服务器根目录 directory.so 然后客户端每次连接时都可以读取和写入文件server.Thank你。
public class SftpServerStarter {
private SshServer sshd;
private final static Logger logger =
LoggerFactory.getLogger(SftpServerStarter.class);
public void start(){
sshd = SshServer.setUpDefaultServer();
sshd.setPort(22);
sshd.setHost("localhost");
sshd.setPasswordAuthenticator(new MyPasswordAuthenticator());
sshd.setPublickeyAuthenticator(new MyPublickeyAuthenticator());
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
sshd.setSubsystemFactories(
Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
sshd.setCommandFactory(new ScpCommandFactory());
try {
logger.info("Starting ...");
sshd.start();
logger.info("Started");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.info("Can not Start Server");
}
}
}
在默认情况下,它采用名为 user.dir
为了改变这一点,您可以覆盖 NativeFileSystemView
和 return 路径中的 getVirtualUserDir()
。
sshd.setFileSystemFactory(new NativeFileSystemFactory() {
@Override
public FileSystemView createFileSystemView(final Session session) {
return new NativeFileSystemView(session.getUsername(), false) {
@Override
public String getVirtualUserDir() {
return "C:\MyRoot";
}
};
};
});
您也可以按照以下link了解如何在不同sshd-core版本的Apache Mina sshd SFTP服务器中设置根目录。
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>0.10.0</version>
</dependency>
进入
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>0.14.0</version>
</dependency>
在较新的 sshd 版本中,您可以使用 org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory
并通过方法 setFileSystemFactory
.
SshServer
实例
片段:
VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory();
fileSystemFactory.setDefaultHomeDir("home.directory");
sshd.setFileSystemFactory(fileSystemFactory)