Apache Mina Sshd SetPasswordAuthenticator不执行

时间:2015-04-23 07:12:29

标签: java sftp winscp apache-mina sshd

我已经设置了Apache Mina Sshd SFTP服务器并且我添加了一些用户权限。要测试我使用Winscp但我可以连接服务器而不输入正确的密码和用户名。看起来像 SetPasswordAuthenticator 方法永远不会被调用。所以我想知道如何为我的apache mina sshd SFTP服务器设置用户名和密码以启动安全服务器。

我的服务器代码,

sshd = SshServer.setUpDefaultServer();
    sshd.setPort(2525);
    sshd.setHost("172.xx.xx.xx");
    sshd.setPasswordAuthenticator(new MyPasswordAuthenticator());
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
    sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>> asList(new SftpSubsystem.Factory()));

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

    sshd.setCommandFactory(new ScpCommandFactory());

    sshd.setKeyExchangeFactories(Arrays
            .<NamedFactory<KeyExchange>> asList(new DHG1.Factory()));
    sshd.setCommandFactory(new ScpCommandFactory());
    sshd.setFileSystemFactory(new VirtualFileSystemFactory("C:/root"));

MyPasswordAuthenticator代码,

public class MyPasswordAuthenticator implements PasswordAuthenticator {

public boolean authenticate(String username, String password, ServerSession session) {
    boolean retour = false;

    if ("user".equals(username) && "123".equals(password)) {
        retour = true;
    }

    return retour;
}

}

Maven依赖,

<dependency>
        <groupId>org.apache.mina</groupId>
        <artifactId>mina-core</artifactId>
        <version>2.0.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.sshd</groupId>
        <artifactId>sshd-sftp</artifactId>
        <version>0.11.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.sshd</groupId>
        <artifactId>sshd-core</artifactId>
        <version>0.14.0</version>
    </dependency>

我是Apache Mina Sshd的新手。感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

最后我发现了它出错的地方。通过删除下面的代码段,您可以在服务器上执行 sshd.setPasswordAuthenticator

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

注意:如果您需要更高的安全性,可以使用上面的代码段并进行正确的实施,但只需添加sshd.setPasswordAuthenticator(new MyPasswordAuthenticator());即可为您的服务器设置基本密码验证。