如何在 apache SshServer 中设置每个用户的最大并发登录数

How to set max concurrent logins per user in apache SshServer

我需要限制 apache SshServer 中每个用户允许的并发会话数。我找到了两个对此功能的引用,但它们似乎已过时。
这是 2010 年的原始补丁: https://issues.apache.org/jira/browse/SSHD-95
我还找到了对其用法的引用: http://apache-mina.10907.n7.nabble.com/How-to-set-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")
    }

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

server.apply {
    properties[ServerFactoryManager.MAX_CONCURRENT_SESSIONS] = 50L
}