【问题标题】:limit all users to 1 session将所有用户限制为 1 个会话
【发布时间】:2015-02-15 03:13:54
【问题描述】:

我被要求将 SQL Server 配置为每次登录只允许一个会话。我发现了一些关于创建登录触发器的参考资料,以防止登录建立超过 1 个会话,但我想知道是否有某种方法可以在较低级别定义它,以便此会话限制是默认值,而不是而不是必须在另一个登录中为每个用户定义这个?

我在 stackoverflow 上的“可能已经有你答案的问题”和“类似问题”中看到了很多对这个主题的引用,但到目前为止,要么没有找到或不理解描述我是什么的帖子试图做。我还看到了关于声明式管理框架的参考,它允许您按我认为的策略配置 SQL Server。

我将继续浏览此处的文章以尝试了解这一点,但与此同时...非常感谢您的建议!

【问题讨论】:

  • http://security.stackexchange.com/问这个问题可能会更好
  • 我假设您的意思是 Microsoft SQL Server。
  • 您的所有用户是否共享同一个登录名,他们是个人登录名还是继承自 AD 组?
  • 是的,它是 Windows 2008 R2 Enterprise 上的 Microsoft SQL 2012。很抱歉没有包括在内。此外,该服务器尚未投入生产。我们的托管设施中正在设置一个服务器,作为基于 Web 的库存系统的后端。我遇到了这个要求,旨在通过不允许 1 个登录超过 1 个会话来保护服务器免受 DOS 攻击。这就是我开始搜索的地方。
  • 按照 Bacon Bits 所说的,如果他们共享一个帐户(从 Web 服务器到数据库服务器)并且 Web 服务器无法创建多个会话,您可能会拒绝一起服务。

标签: sql-server security session login limit


【解决方案1】:

在线图书中logon trigger 的示例与我认为您想要的非常接近,我进行了一些更改以使其适用于所有登录。

-- Trigger must be created by a user with 'view server state' permission in order the trigger to have unrestricted access to sys.dm_exec_sessions.
create trigger connection_limit_trigger on all server with execute as self for logon
as
begin
    -- Check whether the caller is a SysAdmin.
    -- Note: The trigger is executing under an elevated security context so we must switch to the caller's context to test their SysAdmin status.
    declare @IsSysAdmin int
    execute as caller
    set @IsSysAdmin = isnull(is_srvrolemember ('sysadmin'), 0)
    revert

    -- If the user is not a SysAdmin and there are already too many non-dormant sessions for this login then 'rollback' the logon.
    -- Note: sys.dm_exec_sessions does not include an entry for this logon attempt.
    if ((@IsSysAdmin = 0) and ((select count(1) from sys.dm_exec_sessions where is_user_process = 1 and status <> 'Dormant' and original_login_name = original_login()) > 1))
    begin
        raiserror('This login has exceeded the maximum of 1 connections to this SQL Server.', 16, 1)
        rollback
    end
end

我已添加检查,因此该限制不适用于 SysAdmin 登录,并且不计算休眠连接池连接。有几点需要注意;

  • 如果连接未正确关闭,它可能会在 sys.dm_exec_sessions 中停留一段时间,在这种情况下,用户将无法重新连接,直到死连接自行清除。
  • 如果您弄乱了登录触发器,您可以将自己(和其他所有人!)锁定在 SQL Server 之外。请参阅logon trigger 页面了解如何重新加入的详细信息:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    相关资源
    最近更新 更多