一,为什么redis6要增加acl功能模块?

什么是acl?

访问控制列表(ACL)是一种基于包过滤的访问控制技术,

它可以根据设定的条件对接口上的数据包进行过滤,允许其通过或丢弃

redis6增加了acl功能模块后,极大的提高了redis的安全性,

使redis更适用于企业级的业务场景

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,linux平台redis6的安装:

请参见这一篇:

  1. https://www.cnblogs.com/architectforest/p/12830056.html

三,查看redis6中acl的帮助

1,acl的命令列表

  1. [root@centos8 bin]# ./redis-cli
  2. 127.0.0.1:6379> acl help
  3. 1) ACL <subcommand> arg arg ... arg. Subcommands are:
  4. 2) LOAD -- Reload users from the ACL file.
  5. 3) SAVE -- Save the current config to the ACL file.
  6. 4) LIST -- Show user details in config file format.
  7. 5) USERS -- List all the registered usernames.
  8. 6) SETUSER <username> [attribs ...] -- Create or modify a user.
  9. 7) GETUSER <username> -- Get the user details.
  10. 8) DELUSER <username> [...] -- Delete a list of users.
  11. 9) CAT -- List available categories.
  12. 10) CAT <category> -- List commands inside category.
  13. 11) GENPASS [<bits>] -- Generate a secure user password.
  14. 12) WHOAMI -- Return the current connection username.
  15. 13) LOG [<count> | RESET] -- Show the ACL log entries.

以上各个命令的说明:

LOAD:从acl文件加载规则(说明:此文件在redis.conf中有定义)

SAVE:保存规则到acl文件

LIST                  列出所有的acl规则

USERS              列出所有的用户

SETUSER          设置/创建一个用户

GETUSER          查看一个用户

DELUSER            删除一个用户

CAT                      查看所有的命令分类

CAT <category>    列出一个命令分类下的命令

GENPASS   生成密码

WHOAMI     查看当前用户

LOG    日志操作

2,acl命令的参数:密码

>pwd  添加密码列表

<pwd  移除密码列表

nopass 移除当前用户的密码

3,acl命令的参数: command

+<command> 添加命令

-<command> 移除命令

+@<category> 添加一个命令分类

-@<category> 移除一个命令分类

allcommands 所有命令可用 别名 +@all

nocommands 所有命令不可用 别名 -@all

说明:关于​命令分类

所有命令分类可以用 acl cat 查看

命令分类下的命令可以用 acl cat <category>查看

4,acl命令的参数: key

~<pattern>

~* 所有key

allkeys 别名 ~*

四,redis6的acl使用例子:

1,列出当前的acl规则:

  1. 127.0.0.1:6379> acl list
  2. 1) "user default on nopass ~* +@all"

acl的字段说明:

user  :用户
default  :默认用户(反之 为自己创建的用户)
on         :状态是激活,未激活是off
nopass  :不需要密码
~*           :可访问的key
+@all      :可操作的command

2,列出所有的命令分类:

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

3,列出一个命令分类下面所有的命令:

  1. 127.0.0.1:6379> acl cat set
  2. 1) "smembers"
  3. 2) "sinter"
  4. 3) "srandmember"
  5. 4) "sdiff"
  6. 5) "sismember"
  7. 6) "sinterstore"
  8. 7) "scard"
  9. 8) "sadd"
  10. 9) "sort"
  11. 10) "srem"
  12. 11) "sdiffstore"
  13. 12) "sunionstore"
  14. 13) "spop"
  15. 14) "smove"
  16. 15) "sscan"
  17. 16) "sunion"

4,创建用户/查看用户

创建用户

  1. 127.0.0.1:6379> acl setuser laoliu
  2. OK

查看用户

  1. 127.0.0.1:6379> acl getuser laoliu
  2. 1) "flags"
  3. 2) 1) "off"
  4. 3) "passwords"
  5. 4) (empty array)
  6. 5) "commands"
  7. 6) "-@all"
  8. 7) "keys"
  9. 8) (empty array)

说明:可以看到,如果未明确指定,

用户无权使用任何命令,也无权访问任何key

查看添加用户后的acl

  1. 127.0.0.1:6379> acl list
  2. 1) "user default on nopass ~* +@all"
  3. 2) "user laoliu off -@all"

可以看到默认添加的用户状态是off,

需要把账号设置为激活状态:

  1. 127.0.0.1:6379> acl setuser laoliu on
  2. OK

5,给laoliu账号配置权限:

  1. 127.0.0.1:6379> acl setuser laoliu on >lhdpass1 >lhdpass2 >lhdpass3 +@all ~*
  2. OK
  3. 127.0.0.1:6379> acl getuser laoliu
  4. 1) "flags"
  5. 2) 1) "on"
  6. 2) "allkeys"
  7. 3) "allcommands"
  8. 3) "passwords"
  9. 4) 1) "7ff6ac0bf1fc54b1eb01f65e306a757b91f84a0ea421508945fa3e00917d3fef"
  10. 2) "c989e79ee6baa5bfa95af56810ae3e57be9432e8700771b7bc1fbb3b7e75158b"
  11. 3) "2e634d78bd89dce200e61c6e1d1c97e22607105a01c0b84cd3ee4251e6769bb8"
  12. 5) "commands"
  13. 6) "+@all"
  14. 7) "keys"
  15. 8) 1) "*"

说明:

on     用户为激活状态

>lhdpass1 >lhdpass2 >lhdpass3

添加三个密码,每个密码都可以使用

+@all   可以使用所有权限

~*       可以访问所有的key

6,查看当前用户/切换当前用户

#whoami:   查看当前用户

  1. 127.0.0.1:6379> acl whoami
  2. "default"
  3. 127.0.0.1:6379> auth laoliu lhdpass2
  4. OK
  5. 127.0.0.1:6379> acl whoami
  6. "laoliu"

说明:5.x的auth命令无需用户名,

6.x的auth命令必须指定用户名

7,查看用户列表/删除用户/关闭(冻结)用户/激活用户

#acl users: 列出所有用户

  1. 127.0.0.1:6379> acl users
  2. 1) "default"
  3. 2) "help"
  4. 3) "laoliu"

#deluser: 删除用户

  1. 127.0.0.1:6379> acl deluser help
  2. (integer) 1
  3. 127.0.0.1:6379> acl users
  4. 1) "default"
  5. 2) "laoliu"

#关闭(冻结)用户:设置状态为off即可

  1. 127.0.0.1:6379> acl setuser laoliu off
  2. OK
  3. 127.0.0.1:6379> auth laoliu lhdpass1
  4. (error) WRONGPASS invalid username-password pair

说明:关闭用户后已经不能再以此用户登录

#激活一个关闭的用户:设置状态为on即可

  1. 127.0.0.1:6379> acl setuser laoliu on
  2. OK
  3. 127.0.0.1:6379> auth laoliu lhdpass1
  4. OK

8,保存当前的acl

首先在redis.conf中配置acl文件的启用:

  1. [root@centos8 conf]# vi redis.conf

编辑内容:

  1. aclfile /usr/local/soft/redis6/conf/users.acl

说明:这个acl文件的路径指定,应该用绝对路径

启动redis前,如果users.acl不存在,则先手动生成一个users.acl空文件

  1. [root@centos8 conf]# touch /usr/local/soft/redis6/conf/users.acl

重新启动redis

  1. [root@centos8 bin]# systemctl restart redis6

再次用redis-cli连接到server,

  1. [root@centos8 bin]# ./redis-cli
  2. 127.0.0.1:6379> acl whoami
  3. "default"
  4. 127.0.0.1:6379> acl list
  5. 1) "user default on nopass ~* +@all"
  6. 127.0.0.1:6379> acl save
  7. OK

查看外部文件中所保存的规则:

  1. [root@centos8 conf]# more users.acl
  2. user default on nopass ~* +@all

可以看到,已保存成功

说明:如果当前没有配置使用acl外部文件,

则在保存acl规则时redis会给出报错:

  1. 127.0.0.1:6379> acl save
  2. (error) ERR This Redis instance is not configured to use an ACL file.
  3. You may want to specify users via the ACL SETUSER command and
  4. then issue a CONFIG REWRITE (assuming you have a Redis configuration file set)
  5. in order to store users in the Redis configuration.

9,给缺省用户添加密码:

  1. 127.0.0.1:6379> acl setuser default on >lhdpass1 >lhdpass2 >lhdpass3 +@all ~*
  2. OK
  3. 127.0.0.1:6379> acl getuser default
  4. 1) "flags"
  5. 2) 1) "on"
  6. 2) "allkeys"
  7. 3) "allcommands"
  8. 3) "passwords"
  9. 4) 1) "7ff6ac0bf1fc54b1eb01f65e306a757b91f84a0ea421508945fa3e00917d3fef"
  10. 2) "c989e79ee6baa5bfa95af56810ae3e57be9432e8700771b7bc1fbb3b7e75158b"
  11. 3) "2e634d78bd89dce200e61c6e1d1c97e22607105a01c0b84cd3ee4251e6769bb8"
  12. 5) "commands"
  13. 6) "+@all"
  14. 7) "keys"
  15. 8) 1) "*"

然后保存到acl

  1. 127.0.0.1:6379> acl save
  2. OK

查看保存的acl内容

  1. [root@centos8 conf]# more users.acl
  2. user default on #7ff6ac0bf1fc54b1eb01f65e306a757b91f84a0ea421508945fa3e00917d3fef
    #c989e79ee6baa5bfa95af56810ae3e57be9432e8700771b7bc1fbb3b7e75158b
    #2e634d78bd89dce200e61c6e1d1c97e22607105a01c0b84cd3ee4251e6769bb8 ~* +@all

因为default加了密码,重启后需要auth才能访问

  1. [root@centos8 bin]# systemctl restart redis6.service
  2. [root@centos8 bin]# ./redis-cli
  3. 127.0.0.1:6379> get a
  4. (error) NOAUTH Authentication required.
  5. 127.0.0.1:6379> auth default lhdpass2
  6. OK
  7. 127.0.0.1:6379> get a
  8. "aaaa"

10,添加一个生产环境的用户例子:

给予指定的命令和访问指定的内容

  1. 127.0.0.1:6379> acl setuser api on >apipass1 +get +set ~goods:* ~home:*
  2. OK

说明:创建了用户api,

密码: apipass1

可用的命令有 get set

可以访问的key有 分别以goods:/home:作前缀的两类key

新建3个key供测试用

  1. 127.0.0.1:6379> auth laoliu lhdpass1
  2. OK
  3. 127.0.0.1:6379> set goods:123 cup
  4. OK
  5. 127.0.0.1:6379> set home:345 ad
  6. OK
  7. 127.0.0.1:6379> set list:789 allgoods
  8. OK

切换到新用户

  1. 127.0.0.1:6379> auth api apipass1
  2. OK

注意acl的whoami命令当前用户无权使用,因为未做授权

  1. 127.0.0.1:6379> acl whoami
  2. (error) NOPERM this user has no permissions to run the 'acl' command or its subcommand

可以用get/set命令访问goods:作前缀的key

  1. 127.0.0.1:6379> get goods:123
  2. "cup"
  3. 127.0.0.1:6379> set goods:123 shoes
  4. OK
  5. 127.0.0.1:6379> get goods:123
  6. "shoes"

未被授权的key不能访问

  1. 127.0.0.1:6379> get list:789
  2. (error) NOPERM this user has no permissions to access one of the keys used as arguments

11,command/key/password的增加和减少

查看当前用户:

  1. 127.0.0.1:6379> acl getuser api
  2. 1) "flags"
  3. 2) 1) "on"
  4. 3) "passwords"
  5. 4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
  6. 5) "commands"
  7. 6) "-@all +get +set"
  8. 7) "keys"
  9. 8) 1) "goods:*"
  10. 2) "home:*"

减少命令/增加命令

  1. 127.0.0.1:6379> acl setuser api -set
  2. OK
  3. 127.0.0.1:6379> acl setuser api +hget
  4. OK
  5. 127.0.0.1:6379> acl getuser api
  6. 1) "flags"
  7. 2) 1) "on"
  8. 3) "passwords"
  9. 4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
  10. 5) "commands"
  11. 6) "-@all +hget +get"
  12. 7) "keys"
  13. 8) 1) "goods:*"
  14. 2) "home:*"

减少key/增加key

减少已添加的key,用resetkeys,后跟需要保留的key

  1. 127.0.0.1:6379> acl setuser api resetkeys ~home:*
  2. OK
  3. 127.0.0.1:6379> acl getuser api
  4. 1) "flags"
  5. 2) 1) "on"
  6. 3) "passwords"
  7. 4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
  8. 5) "commands"
  9. 6) "-@all +hget +get"
  10. 7) "keys"
  11. 8) 1) "home:*"

添加可访问的key:直接添加就可以

  1. 127.0.0.1:6379> acl setuser api ~list:*
  2. OK
  3. 127.0.0.1:6379> acl getuser api
  4. 1) "flags"
  5. 2) 1) "on"
  6. 3) "passwords"
  7. 4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
  8. 5) "commands"
  9. 6) "-@all +hget +get"
  10. 7) "keys"
  11. 8) 1) "home:*"
  12. 2) "list:*"

添加密码

# >apipass2 :表示添加一个密码:apipass2

  1. 127.0.0.1:6379> acl setuser api >apipass2
  2. OK
  3. 127.0.0.1:6379> acl getuser api
  4. 1) "flags"
  5. 2) 1) "on"
  6. 3) "passwords"
  7. 4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
  8. 2) "a5255a6217658703a1a427ce3e6690aa335f8ca8f089dd629f54be45c2db3187"
  9. 5) "commands"
  10. 6) "-@all +hget +get"
  11. 7) "keys"
  12. 8) 1) "home:*"
  13. 2) "list:*"

删除密码

# <apipass2 :表示删除apipass2这个密码

  1. 127.0.0.1:6379> acl setuser api <apipass2
  2. OK
  3. 127.0.0.1:6379> acl getuser api
  4. 1) "flags"
  5. 2) 1) "on"
  6. 3) "passwords"
  7. 4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
  8. 5) "commands"
  9. 6) "-@all +hget +get"
  10. 7) "keys"
  11. 8) 1) "home:*"
  12. 2) "list:*"

12,acl日志的操作:

清空日志

#log reset: 清空已记录的日志

  1. 127.0.0.1:6379> acl log reset
  2. OK

#log count: 查看指定条数的日志

  1. 127.0.0.1:6379> acl log 5

五,查看redis的版本

  1. [root@centos8 bin]# /usr/local/soft/redis6/bin/redis-server --version
  2. Redis server v=6.0.1 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=0

六,查看centos的版本

  1. [root@centos8 bin]# cat /etc/redhat-release
  2. CentOS Linux release 8.1.1911 (Core)

centos8平台:举例讲解redis6的ACL功能(redis6.0.1)的更多相关文章

  1. centos8平台安装redis6.0.1

    一,redis的官网: https://redis.io/ redis6于5月3日正式发布,它的新增功能: acl 多线程io cluster proxy resp3协议 本文演示redis6.0.1 ...

  2. BS开发平台,一小时搞定功能强大的统计分析页面

    BS开发平台,一小时搞定功能强大的统计分析页面,拥有强大的功能和详细的权限控制. 1.组织数据,分析需求(实际耗时大约20分钟)  2.建立需要的业务数据表(大致10分钟)3. 运行代码工具,生产需要 ...

  3. ZH奶酪:基于ionic.io平台的ionic消息推送功能实现

    Hybrid App越来越火,Ionic的框架也逐渐被更多的人熟知. 在mobile app中,消息推送是很必要的一个功能. 国内很多ionic应用的推送都是用的极光推送,最近研究了一下Ionic自己 ...

  4. 微信开放平台PC端扫码登录功能个人总结

    最近公司给我安排一个微信登录的功能,需求是这样的: 1.登录授权 点击二维码图标后,登录界面切换为如下样式(二维码),微信扫描二维码并授权,即可成功登录:    若当前账号未绑定微信账号,扫描后提示“ ...

  5. 讲解开源项目:功能强大的 JS 文件上传库

    本文作者:HelloGitHub-kalifun HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  6. 举例讲解Python中的死锁、可重入锁和互斥锁

    举例讲解Python中的死锁.可重入锁和互斥锁 一.死锁 简单来说,死锁是一个资源被多次调用,而多次调用方都未能释放该资源就会造成死锁,这里结合例子说明下两种常见的死锁情况. 1.迭代死锁 该情况是一 ...

  7. Java生鲜电商平台-电商中"再来一单"功能架构与详细设计(APP/小程序)

    Java生鲜电商平台-电商中"再来一单"功能架构与详细设计(APP/小程序) 说明:在实际的业务场景中(无论是TO B还是TO C)不管是休闲食品.餐饮.水果.日用百货.母婴等高频 ...

  8. 常用正则表达式最强汇总(含Python代码举例讲解+爬虫实战)

    大家好,我是辰哥~ 本文带大家学习正则表达式,并通过python代码举例讲解常用的正则表达式 最后实战爬取小说网页:重点在于爬取的网页通过正则表达式进行解析. 正则表达式语法 Python的re模块( ...

  9. centos8平台使用stress做压力测试

    一,安装stress: 说明:el8的源里面还没有,先用el7的rpm包 [root@centos8 source]# wget https://download-ib01.fedoraproject ...

随机推荐

  1. 感知生命周期的数据 -- LiveData

    感知生命周期的数据 -- LiveData 零. 前言 上篇文章<万物基于Lifecycle> 介绍了整个Lifecycle体系的基石,今天这篇文章咱们来看看Jetpack给我们带来的活着 ...

  2. django.db.utils.InternalError: (1091, "Can't DROP 'cre_time'; check that column/key exists")

    在执行命令python manage.py migrate时报错:django.db.utils.InternalError: (1091, “Can’t DROP ‘cre_time’; check ...

  3. 记一次函数异常(getopt_long)

    前言 以下参考博客以及man手册. getopt_long函数,getopt_long函数包含了getopt函数的功能,并且还可以指定"长参数"(或者说长选项),与getopt函数 ...

  4. vue-element-admin改造接入后台,搭建有来商城youlai-mall前后端分离管理平台

    一. 前言 本篇基于有来商城youlai-mall微服务项目搭建的后台前端管理平台,技术选型Vue+Element-UI实现前后端分离,解决方案选型vue-element-admin.希望通过本篇你可 ...

  5. Java使用ObjectMapper的简单示例

    一.什么是ObjectMapper? ObjectMapper类是Jackson库的主要类,它提供一些功能将数据集或对象转换的实现. 它将使用JsonParser和JsonGenerator实例来实现 ...

  6. python中random库的使用

    基本随机函数 计算机产生随机数是需要随机数种子的,例如 给定一个随机数种子,就能利用梅森旋转算法产生一系列随机序列 每一个数都是随机数,只要随机种子相同,产生的随机数和数之间的关系都是确定的 随机种子 ...

  7. python身体指数BMI

    问题需求 既要输出国际标准也要输出国内标准

  8. RXJAVA之概述

    RXjava是一个异步和基于事件的程序库.RXjava的核心理念是编程风格的的变化,从传统的命令式程序改变到函数响应式编程. RXjava的基本概念: Observable:发射源,即对象产生的地方. ...

  9. 相同宿主机下的dcoker之间通信

    相同宿主机下的dcoker之间通信 docker docker的本质是进程,隔离的资源包括:网卡.回环设备.路由表和 iptables 规则,这些要素构成了一个进程(docker)发起和响应网络请求的 ...

  10. dubbo学习(二)配置dubbo XML方式配置

    provider(生产者) <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo= ...