How to block SFTP remove operations with Apache MINA SSHD

南笙酒味 提交于 2020-01-23 03:08:09

问题


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();

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?

Thanks for help!


回答1:


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.");
}


来源:https://stackoverflow.com/questions/59803046/how-to-block-sftp-remove-operations-with-apache-mina-sshd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!