重构的时候,就直接换了另一个C#客户端,StackExchange.Redis。
private static ConnectionMultiplexer _redis; private static IDatabase _db; private static IServer _server; private static bool needSave = false; private void Init(string host, int port, string pwd, int database) { var options = ConfigurationOptions.Parse(host + ":" + port); options.SyncTimeout = int.MaxValue; options.AllowAdmin = true; if (!string.IsNullOrEmpty(pwd)) { options.Password = pwd; } if (_redis == null) _redis = ConnectionMultiplexer.Connect(options); if (_server == null) _server = _redis.GetServer(host + ":" + port); if (_db == null) _db = _redis.GetDatabase(database); needSave = false; }这里面,可以直接对options对象设置Password属性。于是对该对象进行了包装,后面使用Redis可以这样,在构造函数里边传入PWD即可,比如下面判断用户是否存在的接口:
public static bool HasShopUser(string userName) { bool hasUser = false; ShopUserEntity userEntity; userEntity = null; using (RedisHelper redis = new RedisHelper(HOST, PORT, PWD)) { userEntity = redis.GetShopUserInfo(userName); } if (userEntity != null) { hasUser = true; } return hasUser; }在替换Redis客户端访问类的时候,顺便对之前Redis里面的数据结构进行了一次重构,经过这次重构,速度提升很明显。于是,于是就直接弄到正式环境然后就把设置密码这个事情给忘记了。
当然,部署有Redis的Linux服务器也按照漏洞建议做了登陆限制和修复。
总结其实这是一个很低级的错误,访问Redis没有设置密码(当然也可能是Redis所在的Linux服务器本身没有对登录做限制),也感谢有乌云这么好的一个平台,能够及时发现系统的问题和漏洞,避免出现更大的损失。作为一个码农其实不应该抱有这样的侥幸心理,就像墨菲定律说的那样“会出错的,终将会出错“ 。最后,希望这篇文章能给大家一个提醒和一些帮助。