6

我需要限制 apache SshServer 中每个用户允许的并发会话。我发现了两个对这个功能的引用,但它们似乎已经过时了。
这是 2010 年的原始补丁: https
://issues.apache.org/jira/browse/SSHD-95 我还发现了它的用法参考: http: //apache-mina.10907.n7.nabble.com/如何设置-max-count-connections-in-sshd-service-td44764.html

它指的是 SshServer.setProperty() 方法。我正在使用 sshd-core 2.4.0,而 SshServer 没有这种方法,我看不到任何明显的替代品,也找不到任何关于它发生了什么或我应该如何做的文档现在这个。我仍然在 ServerFactoryManager 中看到 MAX_CONCURRENT_SESSIONS 键,所以我认为该功能仍然存在于某处,但我找不到需要设置它的位置。

这是服务器设置的样子(它用于 SFTP 服务器,但这对于 ahnd 的问题并不重要,我想):

    private val server = SshServer.setUpDefaultServer().apply {
        val sftpSubsystemFactory = SftpSubsystemFactory().apply {
            addSftpEventListener(sftpEventListener)
        }
        port = sftpPort
        host = "localhost"
        keyPairProvider = when {
            sftpKeyname.isEmpty() -> throw IllegalStateException("No key name for SFTP, aborting!")
            sftpKeyname == "NO_RSA" -> {
                log.warn("Explicitly using NO_RSA, sftp encryption is insecure!")
                SimpleGeneratorHostKeyProvider(File("host.ser").toPath())
            }
            else -> KeyPairProvider.wrap(loadKeyPair(sftpKeyname))
        }

        setPasswordAuthenticator { username, password, _ ->
// current evil hack to prevent users from opening more than one session            
if (activeSessions.any { it.username == username }) {
                log.warn("User attempted multiple concurrent sessions!")
                throw IllegalUserStateException("User already has a session!")
            } else {
                log.debug("new session for user $username")
                // throws AuthenticationException
                authenticationService.checkCredentials(username, password)
                true
            }
        }
        subsystemFactories = listOf(sftpSubsystemFactory)
        fileSystemFactory = YellowSftpFilesystemFactory(ftpHome)
        start()
        log.info("SFTP server started on port $port")
    }
4

1 回答 1

1

(根据我的评论)您可以直接设置属性:

server.apply {
    properties[ServerFactoryManager.MAX_CONCURRENT_SESSIONS] = 50L
}
于 2020-06-22T16:30:46.850 回答