MongoDB 3.X 用户权限控制

摘要:

MongoDB 3.0 安全权限访问控制,在添加用户上面3.0版本和之前的版本有很大的区别,这里就说明下3.0的添加用户的方法。

环境、测试:

在安装MongoDB之后,先关闭auth认证,进入查看数据库,只有一个local库,admin库是不存在的:

[email protected]:/usr/local/mongo4# mongo --port=27020
MongoDB shell version: 3.0.4
connecting to: 127.0.0.1:27020/test
2015-06-29T09:31:08.673-0400 I CONTROL  [initandlisten]
> show dbs;
local  0.078GB

现在需要创建一个帐号,该账号需要有grant权限,即:账号管理的授权权限。注意一点,帐号是跟着库走的,所以在指定库里授权,必须也在指定库里验证(auth)。

> use admin
switched to db admin
> db.createUser(
...   {
...     user: "dba",
...     pwd: "dba",
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
...   }
... )
Successfully added user: {
    "user" : "dba",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}

上面加粗的就是执行的命令:

user:用户名

pwd:密码

roles:指定用户的角色,可以用一个空数组给新用户设定空角色;在roles字段,可以指定内置角色和用户定义的角色。role里的角色可以选:

Built-In Roles(内置角色):
1. 数据库用户角色:read、readWrite;
2. 数据库管理角色:dbAdmin、dbOwner、userAdmin;
3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
4. 备份恢复角色:backup、restore;
5. 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
6. 超级用户角色:root
// 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
7. 内部角色:__system

具体角色:

Read:允许用户读取指定数据库
readWrite:允许用户读写指定数据库
dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
root:只在admin数据库中可用。超级账号,超级权限

刚建立了 userAdminAnyDatabase 角色,用来管理用户,可以通过这个角色来创建、删除用户。验证:需要开启auth参数。

[email protected]:/usr/local/mongo4# mongo --port=27020
MongoDB shell version: 3.0.4
connecting to: 127.0.0.1:27020/test
> show dbs;    ####没有验证,导致没权限。
2015-06-29T10:02:16.634-0400 E QUERY    Error: listDatabases failed:{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
    "code" : 13
}
    at Error (<anonymous>)
    at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)
    at shellHelper.show (src/mongo/shell/utils.js:630:33)
    at shellHelper (src/mongo/shell/utils.js:524:36)
    at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47
> use admin        #验证,因为在admin下面添加的帐号,所以要到admin下面验证。
switched to db admin
> db.auth(‘dba‘,‘dba‘)
1
> show dbs;
admin  0.078GB
local  0.078GB
> use test        #在test库里创建帐号
switched to db test
> db.createUser(
...     {
...       user: "zjyr",
...       pwd: "zjyr",
...       roles: [
...          { role: "read", db: "test" }    #只读帐号
...       ]
...     }
... )
Successfully added user: {
    "user" : "zjyr",
    "roles" : [
        {
            "role" : "read",
            "db" : "test"
        }
    ]
}
> db.createUser(
...     {
...       user: "zjy",
...       pwd: "zjy",
...       roles: [
...          { role: "readWrite", db: "test" }   #读写帐号
...       ]
...     }
... )
Successfully added user: {
    "user" : "zjy",
    "roles" : [
        {
            "role" : "readWrite",                #读写账号
            "db" : "test"
        }
    ]
}
> show users;                                    #查看当前库下的用户
{
    "_id" : "test.zjyr",
    "user" : "zjyr",
    "db" : "test",
    "roles" : [
        {
            "role" : "read",
            "db" : "test"
        }
    ]
}
{
    "_id" : "test.zjy",
    "user" : "zjy",
    "db" : "test",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "test"
        }
    ]
}

上面创建了2个帐号,现在验证下:验证前提需要一个集合

> db.abc.insert({"a":1,"b":2})              #插入失败,没有权限,userAdminAnyDatabase 权限只是针对用户管理的,对其他是没有权限的。
WriteResult({
    "writeError" : {
        "code" : 13,
        "errmsg" : "not authorized on test to execute command { insert: \"abc\", documents: [ { _id: ObjectId(‘55915185d629831d887ce2cb‘), a: 1.0, b: 2.0 } ], ordered: true }"
    }
})
>
bye
[email protected]:/usr/local/mongo4# mongo --port=27020
MongoDB shell version: 3.0.4
connecting to: 127.0.0.1:27020/test
> use test
switched to db test
> db.auth(‘zjy‘,‘zjy‘)       #用创建的readWrite帐号进行写入
1
> db.abc.insert({"a":1,"b":2})
WriteResult({ "nInserted" : 1 })
> db.abc.insert({"a":11,"b":22})
WriteResult({ "nInserted" : 1 })
> db.abc.insert({"a":111,"b":222})
WriteResult({ "nInserted" : 1 })
> db.abc.find()
{ "_id" : ObjectId("559151a1b78649ebd8316853"), "a" : 1, "b" : 2 }
{ "_id" : ObjectId("559151cab78649ebd8316854"), "a" : 11, "b" : 22 }
{ "_id" : ObjectId("559151ceb78649ebd8316855"), "a" : 111, "b" : 222 }
> db.auth(‘zjyr‘,‘zjyr‘)       #切换到只有read权限的帐号
1
> db.abc.insert({"a":1111,"b":2222})  #不能写入
WriteResult({
    "writeError" : {
        "code" : 13,
        "errmsg" : "not authorized on test to execute command { insert: \"abc\", documents: [ { _id: ObjectId(‘559151ebb78649ebd8316856‘), a: 1111.0, b: 2222.0 } ], ordered: true }"
    }
})
> db.abc.find()        #可以查看
{ "_id" : ObjectId("559151a1b78649ebd8316853"), "a" : 1, "b" : 2 }
{ "_id" : ObjectId("559151cab78649ebd8316854"), "a" : 11, "b" : 22 }
{ "_id" : ObjectId("559151ceb78649ebd8316855"), "a" : 111, "b" : 222 }

有没有一个超级权限?不仅可以授权,而且也可以对集合进行任意操作?答案是肯定的,只是不建议使用。那就是role角色设置成root

> db.auth(‘dba‘,‘dba‘)
1
> db.createUser(
...  {
...    user: "zhoujinyi",
...    pwd: "zhoujinyi",
...    roles: [
...       { role: "root", db: "admin" }      #超级root帐号
...    ]
...  }
... )
Successfully added user: {
    "user" : "zhoujinyi",
    "roles" : [
        {
            "role" : "root",
            "db" : "admin"
        }
    ]
}
>
> show users;              #查看当前库下的用户
{
    "_id" : "admin.dba",
    "user" : "dba",
    "db" : "admin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}
{
    "_id" : "admin.zhoujinyi",
    "user" : "zhoujinyi",
    "db" : "admin",
    "roles" : [
        {
            "role" : "root",
            "db" : "admin"
        }
    ]
}
> use admin
switched to db admin
> db.auth(‘zhoujinyi‘,‘zhoujinyi‘)
1
> use test
switched to db test
> db.abc.insert({"a":1,"b":2})
WriteResult({ "nInserted" : 1 })
> db.abc.insert({"a":1111,"b":2222})          #权限都有
WriteResult({ "nInserted" : 1 })
> db.abc.find()
{ "_id" : ObjectId("5591539bb78649ebd8316857"), "a" : 1, "b" : 2 }
{ "_id" : ObjectId("559153a0b78649ebd8316858"), "a" : 1111, "b" : 2222 }
> db.abc.remove({})
WriteResult({ "nRemoved" : 2 })

因为帐号都是在当前需要授权的数据库下授权的,那要是不在当前数据库下会怎么样?

> db
admin
> db.createUser(
...  {
...    user: "dxy",
...    pwd: "dxy",
...    roles: [
...       { role: "readWrite", db: "test" },     #在当前库下创建其他库的帐号,在admin库下创建test、abc库的帐号
...       { role: "readWrite", db: "abc" }
...    ]
...  }
... )
Successfully added user: {
    "user" : "dxy",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "test"
        },
        {
            "role" : "readWrite",
            "db" : "abc"
        }
    ]
}
>
> show users;
{
    "_id" : "admin.dba",
    "user" : "dba",
    "db" : "admin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}
{
    "_id" : "admin.zhoujinyi",
    "user" : "zhoujinyi",
    "db" : "admin",
    "roles" : [
        {
            "role" : "root",
            "db" : "admin"
        }
    ]
}
{
    "_id" : "admin.dxy",
    "user" : "dxy",
    "db" : "admin",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "test"
        },
        {
            "role" : "readWrite",
            "db" : "abc"
        }
    ]
}
> use test
switched to db test
> db.auth(‘dxy‘,‘dxy‘)          #在admin下创建的帐号,不能直接在其他库验证,
Error: 18 Authentication failed.
0
> use admin
switched to db admin            #只能在帐号创建库下认证,再去其他库进行操作。
> db.auth(‘dxy‘,‘dxy‘)
1
> use test
switched to db test
> db.abc.insert({"a":1111,"b":2222})
WriteResult({ "nInserted" : 1 })
> use abc
switched to db abc
> db.abc.insert({"a":1111,"b":2222})
WriteResult({ "nInserted" : 1 })

上面更加进一步说明数据库帐号是跟着数据库来走的,哪里创建哪里认证。

创建了这么多帐号,怎么查看所有帐号

>  use admin
switched to db admin
> db.auth(‘dba‘,‘dba‘)
1
> db.system.users.find().pretty()
{
    "_id" : "admin.dba",
    "user" : "dba",
    "db" : "admin",
    "credentials" : {
        "SCRAM-SHA-1" : {
            "iterationCount" : 10000,
            "salt" : "KfDUzCOIUo7WVjFr64ZOcQ==",
            "storedKey" : "t4sPsKG2dXnZztVYj5EgdUzT9sc=",
            "serverKey" : "2vCGiq9NIc1zKqeEL6VvO4rP26A="
        }
    },
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}
{
    "_id" : "test.zjyr",
    "user" : "zjyr",
    "db" : "test",
    "credentials" : {
        "SCRAM-SHA-1" : {
            "iterationCount" : 10000,
            "salt" : "h1gOW3J7wzJuTqgmmQgJKQ==",
            "storedKey" : "7lkoANdxM2py0qiDBzFaZYPp1cM=",
            "serverKey" : "Qyu6IRNyaKLUvqJ2CAa/tQYY36c="
        }
    },
    "roles" : [
        {
            "role" : "read",
            "db" : "test"
        }
    ]
}
{
    "_id" : "test.zjy",
    "user" : "zjy",
    "db" : "test",
    "credentials" : {
        "SCRAM-SHA-1" : {
            "iterationCount" : 10000,
            "salt" : "afwaKuTYPWwbDBduQ4Hm7g==",
            "storedKey" : "ebb2LYLn4hiOVlZqgrAKBdStfn8=",
            "serverKey" : "LG2qWwuuV+FNMmr9lWs+Rb3DIhQ="
        }
    },
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "test"
        }
    ]
}
{
    "_id" : "admin.zhoujinyi",
    "user" : "zhoujinyi",
    "db" : "admin",
    "credentials" : {
        "SCRAM-SHA-1" : {
            "iterationCount" : 10000,
            "salt" : "pE2cSOYtBOYevk8tqrwbSQ==",
            "storedKey" : "TwMxdnlB5Eiaqg4tNh9ByNuUp9A=",
            "serverKey" : "Mofr9ohVlFfR6/md4LMRkOhXouc="
        }
    },
    "roles" : [
        {
            "role" : "root",
            "db" : "admin"
        }
    ]
}
{
    "_id" : "admin.dxy",
    "user" : "dxy",
    "db" : "admin",
    "credentials" : {
        "SCRAM-SHA-1" : {
            "iterationCount" : 10000,
            "salt" : "XD6smcWX4tdg/ZJPoLxxRg==",
            "storedKey" : "F4uiayykHDp/r9krAKZjdr+gqjM=",
            "serverKey" : "Kf51IU9J3RIrB8CFn5Z5hEKMSkw="
        }
    },
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "test"
        },
        {
            "role" : "readWrite",
            "db" : "abc"
        }
    ]
}
> db.system.users.find().count()
5

备份还原使用那个角色的帐号?之前创建的帐号zjy:test库读写权限;zjyr:test库读权限

[email protected]:~# mongodump --port=27020 -uzjyr -pzjyr --db=test -o backup   #只要读权限就可以备份
2015-06-29T11:20:04.864-0400    writing test.abc to backup/test/abc.bson
2015-06-29T11:20:04.865-0400    writing test.abc metadata to backup/test/abc.metadata.json
2015-06-29T11:20:04.866-0400    done dumping test.abc
2015-06-29T11:20:04.867-0400    writing test.system.indexes to backup/test/system.indexes.bson

[email protected]:~# mongorestore --port=27020 -uzjy -pzjy --db=test backup/test/  #读写权限可以进行还原
2015-06-29T11:20:26.607-0400    building a list of collections to restore from backup/test/ dir
2015-06-29T11:20:26.609-0400    reading metadata file from backup/test/abc.metadata.json
2015-06-29T11:20:26.609-0400    restoring test.abc from file backup/test/abc.bson
2015-06-29T11:20:26.611-0400    error: E11000 duplicate key error index: test.abc.$_id_ dup key: { : ObjectId(‘559154efb78649ebd831685a‘) }
2015-06-29T11:20:26.611-0400    restoring indexes for collection test.abc from metadata
2015-06-29T11:20:26.612-0400    finished restoring test.abc
2015-06-29T11:20:26.612-0400    done
root@zhoujinyi:/usr/local/mongo4# mongo --port=27020
MongoDB shell version: 3.0.4
connecting to: 127.0.0.1:27020/test
> show dbs;    ####没有验证,导致没权限。
2015-06-29T10:02:16.634-0400 E QUERY    Error: listDatabases failed:{
    "ok" : 0,
    "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
    "code" : 13
}
    at Error (<anonymous>)
    at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)
    at shellHelper.show (src/mongo/shell/utils.js:630:33)
    at shellHelper (src/mongo/shell/utils.js:524:36)
    at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47
> use admin        #验证,因为在admin下面添加的帐号,所以要到admin下面验证。
switched to db admin
> db.auth(‘dba‘,‘dba‘)
1
> show dbs;
admin  0.078GB
local  0.078GB
> use test        #在test库里创建帐号
switched to db test
> db.createUser(
...     {
...       user: "zjyr",
...       pwd: "zjyr",
...       roles: [
...          { role: "read", db: "test" }    #只读帐号
...       ]
...     }
... )
Successfully added user: {
    "user" : "zjyr",
    "roles" : [
        {
            "role" : "read",
            "db" : "test"
        }
    ]
}
> db.createUser(
...     {
...       user: "zjy",
...       pwd: "zjy",
...       roles: [
...          { role: "readWrite", db: "test" }   #读写帐号
...       ]
...     }
... )
Successfully added user: {
    "user" : "zjy",
    "roles" : [
        {
            "role" : "readWrite",                #读写账号
            "db" : "test"
        }
    ]
}
> show users;                                    #查看当前库下的用户
{
    "_id" : "test.zjyr",
    "user" : "zjyr",
    "db" : "test",
    "roles" : [
        {
            "role" : "read",
            "db" : "test"
        }
    ]
}
{
    "_id" : "test.zjy",
    "user" : "zjy",
    "db" : "test",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "test"
        }
    ]
}
时间: 2024-12-10 02:39:49

MongoDB 3.X 用户权限控制的相关文章

MongoDB下配置用户权限

MongoDB默认设置为无权限访问限制 注:研究成果基于Windows平台 在部署mongodb成功后,进入控制台: 输入命令:mongod  use admin,你会发现该DB下包含了一个system.user表,呵呵,没错,这个表就等同于MsSql中的用户表,用来存放超级管理员的,那我们就往它里面添加一个超级管理员试试看 里我添加一个超级管理员用户,username为admin,password也为admin,即然我们添加了超级管理员,那咱们就来测试下,看看咱们再次连接MongoDB需不需要

2.4.3、Django用户权限控制

上面已经实现了登录和注销功能,但是还没起作用. 我们需要在每个视图函数里加上限制@login_required 如下: from django.contrib.auth.decorators import login_required ... ... @login_required def data_daka(request): all = class.objects.all() ... ... login_required实现了如下功能: 如果用户没有登录, 重定向到/accounts/log

用户权限控制之命令篇

背景:如果数据库启动没有启用权限控制,那么只要通过IP,PORT就能连上,危险性很高,所有要设置用户访问权限.一.在未设置权限状态下,增加访问用户.> use adminswitched to db admin>db.system.users.find()> db.createUser( {user:"myUserAdmin",pwd:"abc123",roles:[{role:"readWriteAnyDatabase",db

springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制【转】

项目结构: 1.maven项目的pom中引入shiro所需的jar包依赖关系 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <dependency>     <groupid>javax.servlet</groupid>     javax.servlet-api</artifactid>     <version>3.0.1</version>

mongodb 3.2 用户权限管理配置

环境 MongoDB shell version: 3.2.6 Win 7 设置方法 用户权限设置 1.进入mongodb的shell : mongo 2.切换数据库: use admin 从3.0 版本起,默认只有 local 库,没有admin 库,需要我们自己来创建. 3.添加用户,指定用户的角色和数据库: db.createUser( { user: "admin", customData:{description:"superuser"}, pwd: &q

如何通过excel实现用户权限控制

在程序设计中,为了让程序更好的适应每个用户的不同需求,我们经常需要给角色设置一定权限,权限的设置有很多的方式,这篇文章主要介绍一下如何通过excel表格来设置用户权限. 我们现在要实现一个功能,不同用户登录以后,下拉框中显示不同的城市,这里的城市是已经配置好的城市. 原来的城市是没有添加任何权限的,显示效果如下: 实现代码如下: ProjectCityWrapperCollection projectCityCollection = ExternalDataHelper.GetProjectCi

Mysql用户权限控制(5.7以上版本)

1.1. 最简单的MySql权限   最简单也是最高效的,如果解决新手们删库跑路的问题其实也是很简单的,对于正式库只给一个增删改查的权限,或者只给一个查询权限(是不是就解决了删库的可能性?) 以下内容如果看官是大牛,请稍安勿躁,我讲内容的方式是从简单到入门,从入门到进阶,从进阶到实战,从实战到...(包你满意) 使用Root用户,执行 grant SELECT on mall.* TO 'dev'@'192.168.244.%' IDENTIFIED BY '123' WITH GRANT OP

mongodb 用户权限控制

MongoDB已经使用很长一段时间了,基于MongoDB的数据存储也一直没有使用到权限访问(MongoDB默认设置为无权限访问限制),今天特地花了一点时间研究了一下,研究成果如下: 注:研究成果基于Windows平台 MongoDB在本机安装部署好后 1. 输入命令:show dbs,你会发现它内置有两个数据库,一个名为admin,一个名为local.local好像没啥用,如果哪位在使用过程中发现了这个local表的用途,希望能够留言提醒,那我们就专心来说说admin表 2. 输入命令:use

Oracle系列:(26)用户权限控制

1.用户 Oracle中的用户分为二大类 1)Oracle数据库服务器创建时,由系统自动创建的用户,叫系统用户,如sys. 2)利用系统用户创建的用户,叫普通用户,如scott,hr,c##tiger,zhaojun,... >用sys登录,查询当前Oracle数据库服务器中已有用户的名字和状态 username表示登录名 expired&locked表示帐号过期和锁定 open表示帐号现在可用   sqlplus / as sysdba;   col username for a30;