• 基于mssql 触发器的访问权限设置


    以下来源于工作中真实使用到的案例,具体ip信息有变化,其他均无问题,同学们可以参考测试后使用。欢迎交流通过

    基于mssql 触发器的访问权限设置,过程需要2步骤完成

    1、在master库中新建3张记录表
    2、创建触发器
    

     一、新建表

    USE [master]
    GO
    
    /****** Object:  Table [dbo].[Login_Info]    Script Date: 12/02/2020 16:19:48 ******/
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE TABLE [dbo].[Login_Info](
        [Login_Name] [nvarchar](256) NULL,
        [Login_Time] [datetime] NULL,
        [Host_Name] [nvarchar](128) NULL,
        [ValidIP] [nvarchar](128) NULL
    ) ON [PRIMARY]
    
    GO
    
    
    
    USE [master]
    GO
    
    /****** Object:  Table [dbo].[LogonLog]    Script Date: 12/02/2020 16:19:59 ******/
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    SET ANSI_PADDING ON
    GO
    
    CREATE TABLE [dbo].[LogonLog](
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [session_id] [smallint] NULL,
        [login_time] [datetime] NULL,
        [host_name] [nvarchar](128) NULL,
        [original_login_name] [nvarchar](128) NULL,
        [client_net_address] [varchar](48) NULL,
     CONSTRAINT [PK_LogonLog] PRIMARY KEY CLUSTERED 
    (
        [Id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    
    SET ANSI_PADDING OFF
    GO
    
    USE [master]
    GO
    
    /****** Object:  Table [dbo].[ValidLogOn]    Script Date: 12/02/2020 16:20:08 ******/
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE TABLE [dbo].[ValidLogOn](
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [LoginName] [sysname] NOT NULL,
        [ValidIP] [nvarchar](15) NOT NULL,
     CONSTRAINT [PK_ValidLogOn] PRIMARY KEY CLUSTERED 
    (
        [Id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    

     二、新建触发器

    /****** Object:  DdlTrigger [tr_logon_CheckLogOn]    Script Date: 12/02/2020 14:09:59 ******/
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    
    
    
    CREATE TRIGGER [tr_logon_CheckLogOn]
    ON ALL SERVER WITH EXECUTE AS 'sa'
    FOR LOGON
    AS
    BEGIN
    	SET CONCAT_NULL_YIELDS_NULL, ANSI_PADDING, ANSI_WARNINGS ON;
        DECLARE @LoginName sysname
        DECLARE @IP NVARCHAR(15)
        SET @LoginName = ORIGINAL_LOGIN();
        SET @IP = (SELECT EVENTDATA().value('(/EVENT_INSTANCE/ClientHost)[1]', 'NVARCHAR(15)')); 
        
        --全部登陆记录
        --INSERT INTO [master].[dbo].[Login_Info]
        --   SELECT ORIGINAL_LOGIN(), GETDATE(),HOST_NAME,EVENTDATA().value('(/EVENT_INSTANCE/ClientHost)[1]','NVARCHAR(128)')
        --   FROM MASTER.sys.dm_exec_sessions a WHERE a.session_id = @@SPID 
        --判断登录名和IP
        IF NOT EXISTS(SELECT [ValidIP] FROM [master].[dbo].[ValidLogOn] WHERE [LoginName] = @LoginName AND [ValidIP] = @IP)
        BEGIN
        		IF(SUBSTRING(@IP,1,11) != '192.168.1.')  and (@IP != '127.0.0.1') and (@IP != '<local machine>')
        		BEGIN
            ROLLBACK;
            --日志记录
            INSERT INTO [master].[dbo].[LogonLog]
                ([session_id]
                ,[login_time]
                ,[host_name]
                ,[original_login_name]
                ,[client_net_address])
            SELECT 
                a.[session_id],a.[login_time],a.[host_name],
                a.[original_login_name],b.[client_net_address]
                FROM MASTER.sys.dm_exec_sessions a 
                INNER JOIN MASTER.sys.dm_exec_connections b 
                ON a.session_id=b.session_id
                WHERE a.session_id = @@SPID
            END
        END
    END;
    
    
    
    GO
    
    SET ANSI_NULLS OFF
    GO
    
    SET QUOTED_IDENTIFIER OFF
    GO
    
    ENABLE TRIGGER [tr_logon_CheckLogOn] ON ALL SERVER
    GO
    

      

      

  • 相关阅读:
    能让你少写1000行代码的20个正则表达式
    无法识别特性“configProtectionProvider”的解决方案
    C# 对 App.config的appSettings节点数据进行加密
    SQL数据库分配权限
    在C#项目中需要用double类型操作MSSQL float类型数据(附C#数据类型和SQL数据类型对照)
    Linux一键安装web环境全攻略phpstudy版
    阿里云linux服务器到期后续费,网站打不开解决方法之一
    onethink上传到服务器(或者迁移)后台登录验证码错误问题
    PHPCMS网站迁移过程后,添加内容 报500错误解决方案
    css3 media媒体查询器用法总结
  • 原文地址:https://www.cnblogs.com/xzlive/p/14086257.html
Copyright © 2020-2023  润新知