• redis 用户权限管理


     ads

    关注以下公众号查看更多文章

    redis6 可以使用acl命令创建用户分配权限,还可以支持操作key的范围

    创建一个用户test2 并指定密码 123456

    acl setuser test2 >123456

    查看可以分配的权限组

    acl cat

    返回的列表是这样的

    1. 1) "keyspace"
    2. 2) "read"
    3. 3) "write"
    4. 4) "set"
    5. 5) "sortedset"
    6. 6) "list"
    7. 7) "hash"
    8. 8) "string"
    9. 9) "bitmap"
    10. 10) "hyperloglog"
    11. 11) "geo"
    12. 12) "stream"
    13. 13) "pubsub"
    14. 14) "admin"
    15. 15) "fast"
    16. 16) "slow"
    17. 17) "blocking"
    18. 18) "dangerous"
    19. 19) "connection"
    20. 20) "transaction"
    21. 21) "scripting"

    查看权限组下具体包含的命令有哪些

    acl cat keyspace

    返回如下

    1. 1) "expireat"
    2. 2) "randomkey"
    3. 3) "restore-asking"
    4. 4) "pexpire"
    5. 5) "flushall"
    6. 6) "dump"
    7. 7) "persist"
    8. 8) "keys"
    9. 9) "exists"
    10. 10) "expiretime"
    11. 11) "migrate"
    12. 12) "pexpiretime"
    13. 13) "move"
    14. 14) "ttl"
    15. 15) "pexpireat"
    16. 16) "dbsize"
    17. 17) "object|freq"
    18. 18) "object|encoding"
    19. 19) "object|idletime"
    20. 20) "object|help"
    21. 21) "object|refcount"
    22. 22) "swapdb"
    23. 23) "pttl"
    24. 24) "expire"
    25. 25) "unlink"
    26. 26) "type"
    27. 27) "scan"
    28. 28) "renamenx"
    29. 29) "touch"
    30. 30) "restore"
    31. 31) "del"
    32. 32) "flushdb"
    33. 33) "copy"
    34. 34) "rename"

    我们想给test2这个用户授予 string 权限组权力,以及     expireat
        pexpireat
        pexpire
        exists
        keys
        ttl
        expire
        unlink 这几个命令的权力,允许操作key的命名格式为 test2:

    acl setuser test2 +@string +expireat +pexpireat +pexpire +exists +keys +ttl +expire +unlink ~test2:*

    看一下test2这个用户的情况

    acl getuser test2

    返回如下

    1. 1) "flags"
    2. 2) 1) "off"
    3. 3) "passwords"
    4. 4) 1) "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92"
    5. 5) "commands"
    6. 6) "-@all +@string +pexpire +keys +exists +ttl +pexpireat +expire +unlink +expireat"
    7. 7) "keys"
    8. 8) "~test2:*"
    9. 9) "channels"
    10. 10) ""
    11. 11) "selectors"
    12. 12) (empty array)

    打开test2这个用户允许登陆

    acl setuser test2 on

    下面我们用test2 这个用户登陆验证权限

    1. ➜ ~ redis-cli
    2. 127.0.0.1:6379> auth test2 123456
    3. OK
    4. 127.0.0.1:6379> keys *
    5. 1) "age"
    6. 127.0.0.1:6379> keys test2*
    7. (empty array)
    8. 127.0.0.1:6379> del age
    9. (error) NOPERM this user has no permissions to run the 'del' command
    10. 127.0.0.1:6379> unlink age
    11. (error) NOPERM this user has no permissions to access one of the keys used as arguments
    12. 127.0.0.1:6379> set test2:age 12
    13. OK
    14. 127.0.0.1:6379> unlink test2:age
    15. (integer) 1
    16. 127.0.0.1:6379> unlink age
    17. (error) NOPERM this user has no permissions to access one of the keys used as arguments
    18. 127.0.0.1:6379>

    =====================================================================

    2022-12-07 补充

    上面设置内容重启redis后无法保存并失效,redis.conf需要配置acl文件位置

    aclfile conf/users.acl

    使用 acl save 把最新acl用户列表保存到 users.acl文件中

  • 相关阅读:
    基于Java的在线问卷调查系统的设计与实现(源码+lw+部署文档+讲解等)
    Win10系统使用pip安装juypter notebook过程记录(安装在系统盘以外的盘)
    Kubernetes(k8s) Web-UI界面(一):部署和访问仪表板(Dashboard)
    简单的网页制作期末作业——电影泰坦尼克号(4页)
    python venv在linux上激活环境无效,没反应
    pnpm install报错 Value of “this“ must be of type URLSearchParams
    数据集的整理和命名和格式转换
    bat文件批量打开应用程序/快捷方式
    模拟信号隔离放大器PCB焊接模块电压电流信号变送0-5v0-10v4-20mA0-20mA1-5v0-12v0-24v
    01. 嵌入式与人工智能是如何结合的?
  • 原文地址:https://blog.csdn.net/fanghailiang2016/article/details/128092705