本文介绍了如何使用Apache MINA SSHD阻止SFTP删除操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Apache Mina SSHD创建自定义sftp服务器。到目前为止,我的代码为:

I am trying to create a custom sftp server using Apache Mina SSHD. My code so far:

 SshServer sshd = SshServer.setUpDefaultServer();
        sshd.setPort(PORT_NUMBER);
        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("keys/private_key.ppk")));

        SftpSubsystemFactory factory = new SftpSubsystemFactory.Builder()
                .build();


        factory.addSftpEventListener(new BasicSftpEventListener());

        sshd.setSubsystemFactories(Collections.singletonList(factory));
        sshd.setShellFactory(new ProcessShellFactory("/bin/sh", "-i", "-l"));
        sshd.start();

如您所见,我实现了我自己的SftpEventListener:

As you can see, I implemented my own SftpEventListener:

public class BasicSftpEventListener implements SftpEventListener {

    @Override
    public void removing(ServerSession session, Path path) throws IOException {
        System.out.println("Removin");
    }

    @Override
    public void removed(ServerSession session, Path path, Throwable thrown) throws IOException {
        System.out.println("removed");
    }

当我想要删除文件时,它将执行删除和删除的监听器,但是

When I want to remove file, it executes my removing and removed listeners, BUT the remove operation proceeds and the file is deleted.

有没有办法阻止这种情况的发生?

Is there a way how to stop this from happening?

感谢帮助!

推荐答案

如果要阻止删除操作,则需要中断<$ c的流程$ c>删除方法,但有例外。这将告诉Mina停止并且不删除文件。我建议为此使用 java.lang.UnsupportedOperationException

If you want to block delete actions, you will need to interrupt the flow of the removing method with an exception. This will tell Mina to stop and not remove the file. I would recommend using java.lang.UnsupportedOperationException for this:

@Override
public void removing(ServerSession session, Path path) throws UnsupportedOperationException{
    throw new UnsupportedActionException("Removing files is not permitted.");
}

这篇关于如何使用Apache MINA SSHD阻止SFTP删除操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 21:16
查看更多