sftp

public static SshServer getSsd(String shareDir)
{
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(9022);
sshd.setFileSystemFactory(new SftpFileSystem(shareDir));
//NO PASSWORDsshd.setPasswordAuthenticator
sshd.setPasswordAuthenticator(new MyPasswordAuthenticator());
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(
"hostkey.ser"));

List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
//NO PASSWORD UserAuthNone.Factory()
userAuthFactories.add(new UserAuthPassword.Factory());
sshd.setUserAuthFactories(userAuthFactories);

sshd.setCommandFactory(new ScpCommandFactory());

List<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>();

namedFactoryList.add(new SftpSubsystem.Factory());
sshd.setSubsystemFactories(namedFactoryList);

return sshd;
}

getSsd(shareDir).start();


public class DircotorSftpHome implements FileSystemView
{
private String currDir;

public DircotorSftpHome(String currDir)
{
this.currDir = currDir;
}


public SshFile getFile(String file) {
return getFile(currDir, file);
}

public SshFile getFile(SshFile baseDir, String file) {
return getFile(baseDir.getAbsolutePath(), file);
}

protected SshFile getFile(String dir, String file) {
// get actual file object
String physicalName = NativeSshFile.getPhysicalName("/",
dir, file, false);
File fileObj = new File(physicalName);

// strip the root directory and return
String userFileName = physicalName.substring("/".length() - 1);
return new SftpFile(physicalName, fileObj, "test");
}


public class SftpFileSystem implements FileSystemFactory
{

private String shareDir;

public SftpFileSystem(String shareDir)
{
this.shareDir = shareDir;
}

@Override
public FileSystemView createFileSystemView(Session session)
throws IOException
{


return new DircotorSftpHome(shareDir);
}

}


public class MyPasswordAuthenticator implements PasswordAuthenticator
{

private Log logger = LogFactory.getInstance(this);

@SuppressWarnings("deprecation")
@Override
public boolean authenticate(String username, String password,
ServerSession session)
{

return false;
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值