Heim >Datenbank >MySQL-Tutorial >SQL Server 精确授权方法

SQL Server 精确授权方法

WBOY
WBOYOriginal
2016-06-07 14:50:481936Durchsuche

参考文献 正文 要想成功访问 SQL Server 数据库中的数据, 我们需要两个方面的授权: 获得准许连接 SQL Server 服务器的权利; 获得访问特定数据库中数据的权利(select, update, delete, create table ...)。 假设,我们准备建立一个 dba 数据库帐户,用来

参考文献


正文

要想成功访问 SQL Server 数据库中的数据, 我们需要两个方面的授权:

  1. 获得准许连接 SQL Server 服务器的权利;
  2. 获得访问特定数据库中数据的权利(select, update, delete, create table ...)。

假设,我们准备建立一个 dba 数据库帐户,用来管理数据库 mydb。

1. 首先在 SQL Server 服务器级别,创建登陆帐户(create login)

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">创建登陆帐户(create login)</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">create</span> login dba <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">with</span> password<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">=</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">abcd1234@</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>, default_database<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">=</span>mydb

登陆帐户名为:“dba”,登陆密码:abcd1234@”,默认连接到的数据库:“mydb”。 这时候,dba 帐户就可以连接到 SQL Server 服务器上了。但是此时还不能 访问数据库中的对象(严格的说,此时 dba 帐户默认是 guest 数据库用户身份, 可以访问 guest 能够访问的数据库对象)。

要使 dba 帐户能够在 mydb 数据库中访问自己需要的对象, 需要在数据库 mydb 中建立一个“数据库用户”,赋予这个“数据库用户” 某些访问权限,并且把登陆帐户“dba” 和这个“数据库用户” 映射起来。 习惯上,“数据库用户” 的名字和 “登陆帐户”的名字相同,即:“dba”。 创建“数据库用户”和建立映射关系只需要一步即可完成:

2. 创建数据库用户(create user):

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">为登陆账户创建数据库用户(create user),在mydb数据库中的security中的user下可以找到新创建的dba</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">create</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,255)">user</span> dba <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">for</span> login dba <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">with</span> default_schema<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">=</span>dbo

并指定数据库用户“dba” 的默认 schema 是“dbo”。这意味着 用户“dba” 在执行“select * from t”,实际上执行的是 “select * from dbo.t”。

3. 通过加入数据库角色,赋予数据库用户“dba”权限:

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">通过加入数据库角色,赋予数据库用户“db_owner”权限</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">exec</span> sp_addrolemember <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">db_owner</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>, <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">dba</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>

此时,dba 就可以全权管理数据库 mydb 中的对象了。

如果想让 SQL Server 登陆帐户“dba”访问多个数据库,比如 mydb2。 可以让 sa 执行下面的语句:

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">让 SQL Server 登陆帐户“dba”访问多个数据库</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">use</span><span style="margin:0px; padding:0px; line-height:1.8"> mydb2
</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">go</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">create</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,255)">user</span> dba <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">for</span> login dba <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">with</span> default_schema<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">=</span><span style="margin:0px; padding:0px; line-height:1.8">dbo
</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">go</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">exec</span> sp_addrolemember <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">db_owner</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>, <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">dba</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">go</span>

此时,dba 就可以有两个数据库 mydb, mydb2 的管理权限了!

完整的代码示例

复制代码
--创建数据库mydb和mydb2

--在mydb和mydb2中创建测试表,默认是dbo这个schema
CREATE TABLE DEPT
       (DEPTNO int primary key,
        DNAME VARCHAR(14),
        LOC VARCHAR(13) );

--插入数据
INSERT INTO DEPT VALUES (101, 'ACCOUNTING', 'NEW YORK');
INSERT INTO DEPT VALUES (201, 'RESEARCH',   'DALLAS');
INSERT INTO DEPT VALUES (301, 'SALES',      'CHICAGO');
INSERT INTO DEPT VALUES (401, 'OPERATIONS', 'BOSTON');


--查看数据库schema, user 的存储过程
select * from sys.database_principals
select * from sys.schemas 
select * from sys.server_principals

--创建登陆帐户(create login)
create login dba with password='abcd1234@', default_database=mydb

--为登陆账户创建数据库用户(create user),在mydb数据库中的security中的user下可以找到新创建的dba
create user dba for login dba with default_schema=dbo

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">通过加入数据库角色,赋予数据库用户“db_owner”权限</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">exec</span> sp_addrolemember <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">db_owner</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>, <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">dba</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">让 SQL Server 登陆帐户“dba”访问多个数据库</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">use</span><span style="margin:0px; padding:0px; line-height:1.8"> mydb2
</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">go</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">create</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,255)">user</span> dba <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">for</span> login dba <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">with</span> default_schema<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">=</span><span style="margin:0px; padding:0px; line-height:1.8">dbo
</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">go</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">exec</span> sp_addrolemember <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">db_owner</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>, <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">dba</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">go</span>

--禁用登陆帐户
alter login dba disable
--启用登陆帐户
alter login dba enable

--登陆帐户改名
alter login dba with name=dba_tom

--登陆帐户改密码: 
alter login dba with password='aabb@ccdd'

--数据库用户改名: 
alter user dba with name=dba_tom

--更改数据库用户 defult_schema: 
alter user dba with default_schema=sales

--删除数据库用户: 
drop user dba

--删除 SQL Server登陆帐户: 
drop login dba
复制代码

使用存储过程来完成用户创建

下面一个实例来说明在sqlserver中如何使用存储过程创建角色,重建登录,以及如何为登录授权等问题。

复制代码
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">/*</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--示例说明
        示例在数据库InsideTSQL2008中创建一个拥有表HR.Employees的所有权限、拥有表Sales.Orders的SELECT权限的角色r_test
    随后创建了一个登录l_test,然后在数据库InsideTSQL2008中为登录l_test创建了用户账户u_test
    同时将用户账户u_test添加到角色r_test中,使其通过权限继承获取了与角色r_test一样的权限
    最后使用DENY语句拒绝了用户账户u_test对表HR.Employees的SELECT权限。
    经过这样的处理,使用l_test登录SQL Server实例后,它只具有表Sales.Orders的select权限和对表HR.Employees出select外的所有权限。
--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">*/</span>


<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">USE</span><span style="margin:0px; padding:0px; line-height:1.8"> InsideTSQL2008

</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">创建角色 r_test</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">EXEC</span> sp_addrole <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">r_test</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">添加登录 l_test,设置密码为pwd,默认数据库为pubs</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">EXEC</span> sp_addlogin <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">l_test</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>,<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">a@cd123</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>,<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">InsideTSQL2008</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">为登录 l_test 在数据库 pubs 中添加安全账户 u_test</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">EXEC</span> sp_grantdbaccess <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">l_test</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>,<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">u_test</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">添加 u_test 为角色 r_test 的成员</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">EXEC</span> sp_addrolemember <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">r_test</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>,<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">u_test</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>


<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">用l_test登陆,发现在SSMS中找不到仍和表,因此执行下述两条语句出错。</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">select</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">*</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">from</span><span style="margin:0px; padding:0px; line-height:1.8"> Sales.Orders
</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">select</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">*</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">from</span><span style="margin:0px; padding:0px; line-height:1.8"> HR.Employees

</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">授予角色 r_test 对 HR.Employees 表的所有权限</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">GRANT</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">ALL</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">ON</span> HR.Employees <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">TO</span><span style="margin:0px; padding:0px; line-height:1.8"> r_test
</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">The ALL permission is deprecated and maintained only for compatibility. </span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">
--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">It DOES NOT imply ALL permissions defined on the entity.</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">
--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">ALL 权限已不再推荐使用,并且只保留用于兼容性目的。它并不表示对实体定义了 ALL 权限。</span>

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">测试可以查询表HR.Employees,但是Sales.Orders无法查询</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">select</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">*</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">from</span><span style="margin:0px; padding:0px; line-height:1.8"> HR.Employees


</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">如果要收回权限,可以使用如下语句。(可选择执行)</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">revoke</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">all</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">on</span> HR.Employees <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">from</span><span style="margin:0px; padding:0px; line-height:1.8"> r_test
</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">ALL 权限已不再推荐使用,并且只保留用于兼容性目的。它并不表示对实体定义了 ALL 权限。</span>


<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">授予角色 r_test 对 Sales.Orders 表的 SELECT 权限</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">GRANT</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">SELECT</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">ON</span> Sales.Orders <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">TO</span><span style="margin:0px; padding:0px; line-height:1.8"> r_test

</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">用l_test登陆,发现可以查询Sales.Orders和HR.Employees两张表</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">select</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">*</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">from</span><span style="margin:0px; padding:0px; line-height:1.8"> Sales.Orders
</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">select</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">*</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">from</span><span style="margin:0px; padding:0px; line-height:1.8"> HR.Employees

</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">拒绝安全账户 u_test 对 HR.Employees 表的 SELECT 权限</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">DENY</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">SELECT</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">ON</span> HR.Employees <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">TO</span><span style="margin:0px; padding:0px; line-height:1.8"> u_test

</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">再次执行查询HR.Employees表的语句,提示:拒绝了对对象 'Employees' (数据库 'InsideTSQL2008',架构 'HR')的 SELECT 权限。</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">select</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">*</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">from</span><span style="margin:0px; padding:0px; line-height:1.8"> HR.Employees

</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">重新授权</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">GRANT</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">SELECT</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">ON</span> HR.Employees <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">TO</span><span style="margin:0px; padding:0px; line-height:1.8"> u_test

</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">再次查询,可以查询出结果。</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">select</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(128,128,128)">*</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">from</span><span style="margin:0px; padding:0px; line-height:1.8"> HR.Employees


</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">USE</span><span style="margin:0px; padding:0px; line-height:1.8"> InsideTSQL2008
</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">从数据库中删除安全账户,failed</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">EXEC</span> sp_revokedbaccess <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">u_test</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">删除角色 r_test,failed</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">EXEC</span> sp_droprole <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">r_test</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">删除登录 l_test,success</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">EXEC</span> sp_droplogin <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">l_test</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span>
复制代码

revoke 与 deny的区别

revoke:收回之前被授予的权限

deny:拒绝给当前数据库内的安全帐户授予权限并防止安全帐户通过其组或角色成员资格继承权限。比如UserA所在的角色组有inset权限,但是我们Deny UserA使其没有insert权限,那么以后即使UserA再怎么到其他含有Insert的角色组中去,还是没有insert权限,除非该用户被显示授权。

简单来说,deny就是将来都不许给,revoke就是收回已经给予的。

实例

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">GRANT</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">INSERT</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">ON</span> TableA <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">TO</span><span style="margin:0px; padding:0px; line-height:1.8"> RoleA
</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">GO</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">EXEC</span> sp_addrolemember RoleA, <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">UserA</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(255,0,0)">'</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)"> 用户UserA将有TableA的INSERT权限</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">GO</span>

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">REVOKE</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">INSERT</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">ON</span> TableA <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">FROM</span> RoleA <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)"> 用户UserA将没有TableA的INSERT权限,收回权限</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">GO</span>

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">GRANT</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">INSERT</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">ON</span> TableA TORoleA <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">重新给RoleA以TableA的INSERT权限</span>
<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">GO</span> 

<span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">DENY</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">INSERT</span> <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">ON</span> TableA <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,0,255)">TO</span> UserA <span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)">--</span><span style="margin:0px; padding:0px; line-height:1.8; color:rgb(0,128,128)"> 虽然用户UserA所在RoleA有TableA的INSERT权限,但UserA本身被DENY了,所以用户UserA将没有TableA的INSERT权限。</span>

参考: http://database.51cto.com/art/201009/224075.htm

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn