[英]How to Set Root Directory in Apache Mina Sshd Server in Java
我使用Apache Mina Sshd API在java中啟動本地 SFTP 服務器。在 SFTP 客戶端中,我使用Jcraft jsch API創建我的 SFTP 客戶端。我成功啟動了一個服務器。問題是我想寫一些單元測試用例來檢查客戶端是否可以將某些文件放入服務器的根目錄。 目前我的 SFTP 服務器沒有任何根目錄。所以我想知道是否有任何方法可以設置服務器的根目錄。
例如: C:\\sftp我如何將此路徑設置為我的服務器根目錄。這樣客戶端每次連接服務器時都可以讀取和寫入文件。謝謝。
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");
}
}
}
在更新的 sshd 版本中,您可以使用org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory
並通過方法setFileSystemFactory
將其提供給SshServer
實例。
片段:
VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory();
fileSystemFactory.setDefaultHomeDir("home.directory");
sshd.setFileSystemFactory(fileSystemFactory)
在默認情況下,它從名為user.dir
System 屬性中獲取根路徑
序來改變這種情況,你可以覆蓋getVirtualUserDir()
在NativeFileSystemView
和返回路徑中。
sshd.setFileSystemFactory(new NativeFileSystemFactory() {
@Override
public FileSystemView createFileSystemView(final Session session) {
return new NativeFileSystemView(session.getUsername(), false) {
@Override
public String getVirtualUserDir() {
return "C:\\MyRoot";
}
};
};
});
您也可以按照以下鏈接了解如何在不同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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.