centos8平台:举例讲解redis6的ACL功能(redis6.0.1)
一,为什么redis6要增加acl功能模块?
什么是acl?
访问控制列表(ACL)是一种基于包过滤的访问控制技术,
它可以根据设定的条件对接口上的数据包进行过滤,允许其通过或丢弃
redis6增加了acl功能模块后,极大的提高了redis的安全性,
使redis更适用于企业级的业务场景
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,linux平台redis6的安装:
请参见这一篇:
- https://www.cnblogs.com/architectforest/p/12830056.html
三,查看redis6中acl的帮助
1,acl的命令列表
- [root@centos8 bin]# ./redis-cli
- 127.0.0.1:6379> acl help
- 1) ACL <subcommand> arg arg ... arg. Subcommands are:
- 2) LOAD -- Reload users from the ACL file.
- 3) SAVE -- Save the current config to the ACL file.
- 4) LIST -- Show user details in config file format.
- 5) USERS -- List all the registered usernames.
- 6) SETUSER <username> [attribs ...] -- Create or modify a user.
- 7) GETUSER <username> -- Get the user details.
- 8) DELUSER <username> [...] -- Delete a list of users.
- 9) CAT -- List available categories.
- 10) CAT <category> -- List commands inside category.
- 11) GENPASS [<bits>] -- Generate a secure user password.
- 12) WHOAMI -- Return the current connection username.
- 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规则:
- 127.0.0.1:6379> acl list
- 1) "user default on nopass ~* +@all"
acl的字段说明:
user :用户
default :默认用户(反之 为自己创建的用户)
on :状态是激活,未激活是off
nopass :不需要密码
~* :可访问的key
+@all :可操作的command
2,列出所有的命令分类:
- 127.0.0.1:6379> acl cat
- 1) "keyspace"
- 2) "read"
- 3) "write"
- 4) "set"
- 5) "sortedset"
- 6) "list"
- 7) "hash"
- 8) "string"
- 9) "bitmap"
- 10) "hyperloglog"
- 11) "geo"
- 12) "stream"
- 13) "pubsub"
- 14) "admin"
- 15) "fast"
- 16) "slow"
- 17) "blocking"
- 18) "dangerous"
- 19) "connection"
- 20) "transaction"
- 21) "scripting"
3,列出一个命令分类下面所有的命令:
- 127.0.0.1:6379> acl cat set
- 1) "smembers"
- 2) "sinter"
- 3) "srandmember"
- 4) "sdiff"
- 5) "sismember"
- 6) "sinterstore"
- 7) "scard"
- 8) "sadd"
- 9) "sort"
- 10) "srem"
- 11) "sdiffstore"
- 12) "sunionstore"
- 13) "spop"
- 14) "smove"
- 15) "sscan"
- 16) "sunion"
4,创建用户/查看用户
创建用户
- 127.0.0.1:6379> acl setuser laoliu
- OK
查看用户
- 127.0.0.1:6379> acl getuser laoliu
- 1) "flags"
- 2) 1) "off"
- 3) "passwords"
- 4) (empty array)
- 5) "commands"
- 6) "-@all"
- 7) "keys"
- 8) (empty array)
说明:可以看到,如果未明确指定,
用户无权使用任何命令,也无权访问任何key
查看添加用户后的acl
- 127.0.0.1:6379> acl list
- 1) "user default on nopass ~* +@all"
- 2) "user laoliu off -@all"
可以看到默认添加的用户状态是off,
需要把账号设置为激活状态:
- 127.0.0.1:6379> acl setuser laoliu on
- OK
5,给laoliu账号配置权限:
- 127.0.0.1:6379> acl setuser laoliu on >lhdpass1 >lhdpass2 >lhdpass3 +@all ~*
- OK
- 127.0.0.1:6379> acl getuser laoliu
- 1) "flags"
- 2) 1) "on"
- 2) "allkeys"
- 3) "allcommands"
- 3) "passwords"
- 4) 1) "7ff6ac0bf1fc54b1eb01f65e306a757b91f84a0ea421508945fa3e00917d3fef"
- 2) "c989e79ee6baa5bfa95af56810ae3e57be9432e8700771b7bc1fbb3b7e75158b"
- 3) "2e634d78bd89dce200e61c6e1d1c97e22607105a01c0b84cd3ee4251e6769bb8"
- 5) "commands"
- 6) "+@all"
- 7) "keys"
- 8) 1) "*"
说明:
on 用户为激活状态
>lhdpass1 >lhdpass2 >lhdpass3
添加三个密码,每个密码都可以使用
+@all 可以使用所有权限
~* 可以访问所有的key
6,查看当前用户/切换当前用户
#whoami: 查看当前用户
- 127.0.0.1:6379> acl whoami
- "default"
- 127.0.0.1:6379> auth laoliu lhdpass2
- OK
- 127.0.0.1:6379> acl whoami
- "laoliu"
说明:5.x的auth命令无需用户名,
6.x的auth命令必须指定用户名
7,查看用户列表/删除用户/关闭(冻结)用户/激活用户
#acl users: 列出所有用户
- 127.0.0.1:6379> acl users
- 1) "default"
- 2) "help"
- 3) "laoliu"
#deluser: 删除用户
- 127.0.0.1:6379> acl deluser help
- (integer) 1
- 127.0.0.1:6379> acl users
- 1) "default"
- 2) "laoliu"
#关闭(冻结)用户:设置状态为off即可
- 127.0.0.1:6379> acl setuser laoliu off
- OK
- 127.0.0.1:6379> auth laoliu lhdpass1
- (error) WRONGPASS invalid username-password pair
说明:关闭用户后已经不能再以此用户登录
#激活一个关闭的用户:设置状态为on即可
- 127.0.0.1:6379> acl setuser laoliu on
- OK
- 127.0.0.1:6379> auth laoliu lhdpass1
- OK
8,保存当前的acl
首先在redis.conf中配置acl文件的启用:
- [root@centos8 conf]# vi redis.conf
编辑内容:
- aclfile /usr/local/soft/redis6/conf/users.acl
说明:这个acl文件的路径指定,应该用绝对路径
启动redis前,如果users.acl不存在,则先手动生成一个users.acl空文件
- [root@centos8 conf]# touch /usr/local/soft/redis6/conf/users.acl
重新启动redis
- [root@centos8 bin]# systemctl restart redis6
再次用redis-cli连接到server,
- [root@centos8 bin]# ./redis-cli
- 127.0.0.1:6379> acl whoami
- "default"
- 127.0.0.1:6379> acl list
- 1) "user default on nopass ~* +@all"
- 127.0.0.1:6379> acl save
- OK
查看外部文件中所保存的规则:
- [root@centos8 conf]# more users.acl
- user default on nopass ~* +@all
可以看到,已保存成功
说明:如果当前没有配置使用acl外部文件,
则在保存acl规则时redis会给出报错:
- 127.0.0.1:6379> acl save
- (error) ERR This Redis instance is not configured to use an ACL file.
- You may want to specify users via the ACL SETUSER command and
- then issue a CONFIG REWRITE (assuming you have a Redis configuration file set)
- in order to store users in the Redis configuration.
9,给缺省用户添加密码:
- 127.0.0.1:6379> acl setuser default on >lhdpass1 >lhdpass2 >lhdpass3 +@all ~*
- OK
- 127.0.0.1:6379> acl getuser default
- 1) "flags"
- 2) 1) "on"
- 2) "allkeys"
- 3) "allcommands"
- 3) "passwords"
- 4) 1) "7ff6ac0bf1fc54b1eb01f65e306a757b91f84a0ea421508945fa3e00917d3fef"
- 2) "c989e79ee6baa5bfa95af56810ae3e57be9432e8700771b7bc1fbb3b7e75158b"
- 3) "2e634d78bd89dce200e61c6e1d1c97e22607105a01c0b84cd3ee4251e6769bb8"
- 5) "commands"
- 6) "+@all"
- 7) "keys"
- 8) 1) "*"
然后保存到acl
- 127.0.0.1:6379> acl save
- OK
查看保存的acl内容
- [root@centos8 conf]# more users.acl
- user default on #7ff6ac0bf1fc54b1eb01f65e306a757b91f84a0ea421508945fa3e00917d3fef
#c989e79ee6baa5bfa95af56810ae3e57be9432e8700771b7bc1fbb3b7e75158b
#2e634d78bd89dce200e61c6e1d1c97e22607105a01c0b84cd3ee4251e6769bb8 ~* +@all
因为default加了密码,重启后需要auth才能访问
- [root@centos8 bin]# systemctl restart redis6.service
- [root@centos8 bin]# ./redis-cli
- 127.0.0.1:6379> get a
- (error) NOAUTH Authentication required.
- 127.0.0.1:6379> auth default lhdpass2
- OK
- 127.0.0.1:6379> get a
- "aaaa"
10,添加一个生产环境的用户例子:
给予指定的命令和访问指定的内容
- 127.0.0.1:6379> acl setuser api on >apipass1 +get +set ~goods:* ~home:*
- OK
说明:创建了用户api,
密码: apipass1
可用的命令有 get set
可以访问的key有 分别以goods:/home:作前缀的两类key
新建3个key供测试用
- 127.0.0.1:6379> auth laoliu lhdpass1
- OK
- 127.0.0.1:6379> set goods:123 cup
- OK
- 127.0.0.1:6379> set home:345 ad
- OK
- 127.0.0.1:6379> set list:789 allgoods
- OK
切换到新用户
- 127.0.0.1:6379> auth api apipass1
- OK
注意acl的whoami命令当前用户无权使用,因为未做授权
- 127.0.0.1:6379> acl whoami
- (error) NOPERM this user has no permissions to run the 'acl' command or its subcommand
可以用get/set命令访问goods:作前缀的key
- 127.0.0.1:6379> get goods:123
- "cup"
- 127.0.0.1:6379> set goods:123 shoes
- OK
- 127.0.0.1:6379> get goods:123
- "shoes"
未被授权的key不能访问
- 127.0.0.1:6379> get list:789
- (error) NOPERM this user has no permissions to access one of the keys used as arguments
11,command/key/password的增加和减少
查看当前用户:
- 127.0.0.1:6379> acl getuser api
- 1) "flags"
- 2) 1) "on"
- 3) "passwords"
- 4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
- 5) "commands"
- 6) "-@all +get +set"
- 7) "keys"
- 8) 1) "goods:*"
- 2) "home:*"
减少命令/增加命令
- 127.0.0.1:6379> acl setuser api -set
- OK
- 127.0.0.1:6379> acl setuser api +hget
- OK
- 127.0.0.1:6379> acl getuser api
- 1) "flags"
- 2) 1) "on"
- 3) "passwords"
- 4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
- 5) "commands"
- 6) "-@all +hget +get"
- 7) "keys"
- 8) 1) "goods:*"
- 2) "home:*"
减少key/增加key
减少已添加的key,用resetkeys,后跟需要保留的key
- 127.0.0.1:6379> acl setuser api resetkeys ~home:*
- OK
- 127.0.0.1:6379> acl getuser api
- 1) "flags"
- 2) 1) "on"
- 3) "passwords"
- 4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
- 5) "commands"
- 6) "-@all +hget +get"
- 7) "keys"
- 8) 1) "home:*"
添加可访问的key:直接添加就可以
- 127.0.0.1:6379> acl setuser api ~list:*
- OK
- 127.0.0.1:6379> acl getuser api
- 1) "flags"
- 2) 1) "on"
- 3) "passwords"
- 4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
- 5) "commands"
- 6) "-@all +hget +get"
- 7) "keys"
- 8) 1) "home:*"
- 2) "list:*"
添加密码
# >apipass2 :表示添加一个密码:apipass2
- 127.0.0.1:6379> acl setuser api >apipass2
- OK
- 127.0.0.1:6379> acl getuser api
- 1) "flags"
- 2) 1) "on"
- 3) "passwords"
- 4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
- 2) "a5255a6217658703a1a427ce3e6690aa335f8ca8f089dd629f54be45c2db3187"
- 5) "commands"
- 6) "-@all +hget +get"
- 7) "keys"
- 8) 1) "home:*"
- 2) "list:*"
删除密码
# <apipass2 :表示删除apipass2这个密码
- 127.0.0.1:6379> acl setuser api <apipass2
- OK
- 127.0.0.1:6379> acl getuser api
- 1) "flags"
- 2) 1) "on"
- 3) "passwords"
- 4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
- 5) "commands"
- 6) "-@all +hget +get"
- 7) "keys"
- 8) 1) "home:*"
- 2) "list:*"
12,acl日志的操作:
清空日志
#log reset: 清空已记录的日志
- 127.0.0.1:6379> acl log reset
- OK
#log count: 查看指定条数的日志
- 127.0.0.1:6379> acl log 5
五,查看redis的版本
- [root@centos8 bin]# /usr/local/soft/redis6/bin/redis-server --version
- Redis server v=6.0.1 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=0
六,查看centos的版本
- [root@centos8 bin]# cat /etc/redhat-release
- CentOS Linux release 8.1.1911 (Core)
centos8平台:举例讲解redis6的ACL功能(redis6.0.1)的更多相关文章
- centos8平台安装redis6.0.1
一,redis的官网: https://redis.io/ redis6于5月3日正式发布,它的新增功能: acl 多线程io cluster proxy resp3协议 本文演示redis6.0.1 ...
- BS开发平台,一小时搞定功能强大的统计分析页面
BS开发平台,一小时搞定功能强大的统计分析页面,拥有强大的功能和详细的权限控制. 1.组织数据,分析需求(实际耗时大约20分钟) 2.建立需要的业务数据表(大致10分钟)3. 运行代码工具,生产需要 ...
- ZH奶酪:基于ionic.io平台的ionic消息推送功能实现
Hybrid App越来越火,Ionic的框架也逐渐被更多的人熟知. 在mobile app中,消息推送是很必要的一个功能. 国内很多ionic应用的推送都是用的极光推送,最近研究了一下Ionic自己 ...
- 微信开放平台PC端扫码登录功能个人总结
最近公司给我安排一个微信登录的功能,需求是这样的: 1.登录授权 点击二维码图标后,登录界面切换为如下样式(二维码),微信扫描二维码并授权,即可成功登录: 若当前账号未绑定微信账号,扫描后提示“ ...
- 讲解开源项目:功能强大的 JS 文件上传库
本文作者:HelloGitHub-kalifun HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...
- 举例讲解Python中的死锁、可重入锁和互斥锁
举例讲解Python中的死锁.可重入锁和互斥锁 一.死锁 简单来说,死锁是一个资源被多次调用,而多次调用方都未能释放该资源就会造成死锁,这里结合例子说明下两种常见的死锁情况. 1.迭代死锁 该情况是一 ...
- Java生鲜电商平台-电商中"再来一单"功能架构与详细设计(APP/小程序)
Java生鲜电商平台-电商中"再来一单"功能架构与详细设计(APP/小程序) 说明:在实际的业务场景中(无论是TO B还是TO C)不管是休闲食品.餐饮.水果.日用百货.母婴等高频 ...
- 常用正则表达式最强汇总(含Python代码举例讲解+爬虫实战)
大家好,我是辰哥~ 本文带大家学习正则表达式,并通过python代码举例讲解常用的正则表达式 最后实战爬取小说网页:重点在于爬取的网页通过正则表达式进行解析. 正则表达式语法 Python的re模块( ...
- centos8平台使用stress做压力测试
一,安装stress: 说明:el8的源里面还没有,先用el7的rpm包 [root@centos8 source]# wget https://download-ib01.fedoraproject ...
随机推荐
- 感知生命周期的数据 -- LiveData
感知生命周期的数据 -- LiveData 零. 前言 上篇文章<万物基于Lifecycle> 介绍了整个Lifecycle体系的基石,今天这篇文章咱们来看看Jetpack给我们带来的活着 ...
- 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 ...
- 记一次函数异常(getopt_long)
前言 以下参考博客以及man手册. getopt_long函数,getopt_long函数包含了getopt函数的功能,并且还可以指定"长参数"(或者说长选项),与getopt函数 ...
- vue-element-admin改造接入后台,搭建有来商城youlai-mall前后端分离管理平台
一. 前言 本篇基于有来商城youlai-mall微服务项目搭建的后台前端管理平台,技术选型Vue+Element-UI实现前后端分离,解决方案选型vue-element-admin.希望通过本篇你可 ...
- Java使用ObjectMapper的简单示例
一.什么是ObjectMapper? ObjectMapper类是Jackson库的主要类,它提供一些功能将数据集或对象转换的实现. 它将使用JsonParser和JsonGenerator实例来实现 ...
- python中random库的使用
基本随机函数 计算机产生随机数是需要随机数种子的,例如 给定一个随机数种子,就能利用梅森旋转算法产生一系列随机序列 每一个数都是随机数,只要随机种子相同,产生的随机数和数之间的关系都是确定的 随机种子 ...
- python身体指数BMI
问题需求 既要输出国际标准也要输出国内标准
- RXJAVA之概述
RXjava是一个异步和基于事件的程序库.RXjava的核心理念是编程风格的的变化,从传统的命令式程序改变到函数响应式编程. RXjava的基本概念: Observable:发射源,即对象产生的地方. ...
- 相同宿主机下的dcoker之间通信
相同宿主机下的dcoker之间通信 docker docker的本质是进程,隔离的资源包括:网卡.回环设备.路由表和 iptables 规则,这些要素构成了一个进程(docker)发起和响应网络请求的 ...
- dubbo学习(二)配置dubbo XML方式配置
provider(生产者) <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo= ...