在redis中设置客户端登录密码的方法

42次阅读
没有评论

共计 4037 个字符,预计需要花费 11 分钟才能阅读完成。

自动写代码机器人,免费开通

这篇文章给大家分享的是有关在 redis 中设置客户端登录密码的方法的内容。丸趣 TV 小编觉得挺实用的,因此分享给大家做个参考,一起跟随丸趣 TV 小编过来看看吧。

导语:

为了保证安全性,redis 在生产环境中一般都会设置登录密码,今天我就来为大家介绍一下如何设置登录密码。

修改 redis.conf

RT,打开 redis.conf 文件,搜索 requirepass 关键字,如下图:

在 redis 中设置客户端登录密码的方法

关注标记的那一行,#requirepass foobared。设置密码的方法就是去掉注释的 #,把 foobared 替换成自己的密码即可,例如将密码设置为 123456:

在 redis 中设置客户端登录密码的方法

修改完成后重启 redis,再次通过 redis 客户端 redis-cli 登录并操作可以发现会报一个身份认证错误:

在 redis 中设置客户端登录密码的方法

这就说明我们已经成功的设置了密码,所以通过客户端连接的话必须加上密码参数才能正常连接:

在 redis 中设置客户端登录密码的方法

如上图所示,加了 - a 参数之后即可正常连接并操作 redis。

jedis 设置密码

当我们用 Java 客户端连接 redis 时会遇到同样的问题,下面看一段简单的 jedis 连接 redis 的测试代码:

package com.firstelite.test;
 
import org.junit.Test;
 
import redis.clients.jedis.Jedis;
 
public class Test4Jedis {
 
 @Test
 public void testTwo() {
 Jedis jedis = new Jedis( 192.168.145.10 
 System.out.println( Connection to server sucessfully 
 //  查看服务是否运行
 System.out.println(Server is running:   + jedis.ping());
 }
 
}

非常简单,仅仅是测试一下 Jedis 是否连通 redis 服务器,运行 junit 后我们发现报异常了:

redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required.
 at redis.clients.jedis.Protocol.processError(Protocol.java:117)
 at redis.clients.jedis.Protocol.process(Protocol.java:142)
 at redis.clients.jedis.Protocol.read(Protocol.java:196)
 at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:288)
 at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:187)
 at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:109)
 at com.firstelite.test.Test4Jedis.testTwo(Test4Jedis.java:15)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:601)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
 at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

显而易见,由于我们设置了密码但在这里又没有指定密码,所以报了和刚才相同的错误,那么如何指定密码呢?很简单,Jedis 的父类 BinaryJedis 提供了这样一样方法:

 public String auth(final String password) { checkIsInMulti();
 client.auth(password);
 return client.getStatusCodeReply();
 }

所以在创建了 Jedis 的实例后再加上一行 jedis.auth(123456 即可,最后看一下运行结果:

在 redis 中设置客户端登录密码的方法

spring-data-redis 设置密码

通常情况下在实际的 java 项目中我们会选择 Spring 提供的 spring-data-redis 来操作 redis,spring 的封装可以给我们提供很多便捷之处。那么 spring-data-redis 又是如何设置密码的呢?首先定义一个 redis.properties 配置文件,定义一组 redis 属性供 spring 加载使用,其中就包含密码(redis.password):

# Redis settings 
redis.host=192.168.145.10 
redis.port=6379 
redis.password=123456
redis.timeout=100000 
redis.maxTotal=300 
redis.maxIdle=100 
redis.maxWaitMillis=1000 
redis.testOnBorrow=true

然后在由 Spring 封装的 JedisConnectionFactory 中来设置密码属性即可,下面是完整 redis 配置:

 !-- redis 配置  --
bean id= poolConfig   >

感谢各位的阅读!关于“在 redis 中设置客户端登录密码的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向 AI 问一下细节

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-16发表,共计4037字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)