前言

redis在生产环境中通常都会设置密码以保证一定的安全性,本篇blog就简单记录一下如何在redis中设置客户端登录密码。

修改redis.conf

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

Redis 设置密码登录_spring

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

Redis 设置密码登录_redis_02

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

Redis 设置密码登录_redis_03

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

Redis 设置密码登录_spring_04

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

jedis设置密码

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



  1. package com.firstelite.test;
  2. import org.junit.Test;

  3. import redis.clients.jedis.Jedis;

  4. public class Test4Jedis {

  5. @Test
  6. public void testTwo() {
  7. Jedis jedis = new Jedis("192.168.145.10");
  8. System.out.println("Connection to server sucessfully");
  9. // 查看服务是否运行
  10. System.out.println("Server is running: " + jedis.ping());
  11. }
  12. }



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


  1. redis.clients.jedis.exceptions.JedisDataException: NOAUTH Authentication required.
  2. at redis.clients.jedis.Protocol.processError(Protocol.java:117)
  3. at redis.clients.jedis.Protocol.process(Protocol.java:142)
  4. at redis.clients.jedis.Protocol.read(Protocol.java:196)
  5. at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:288)
  6. at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:187)
  7. at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:109)
  8. at com.firstelite.test.Test4Jedis.testTwo(Test4Jedis.java:15)
  9. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  10. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  11. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  12. at java.lang.reflect.Method.invoke(Method.java:601)
  13. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
  14. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  15. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
  16. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
  17. at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
  18. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
  19. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
  20. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
  21. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
  22. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
  23. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
  24. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
  25. at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
  26. at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
  27. at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  28. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
  29. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
  30. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
  31. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


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



  1. public String auth(final String password) {
  2. checkIsInMulti();
  3. client.auth(password);
  4. return client.getStatusCodeReply();
  5. }



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

Redis 设置密码登录_spring_05

spring-data-redis设置密码

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



  1. # Redis settings
  2. redis.host=192.168.145.10
  3. redis.port=6379
  4. redis.password=123456
  5. redis.timeout=100000
  6. redis.maxTotal=300
  7. redis.maxIdle=100
  8. redis.maxWaitMillis=1000
  9. redis.testOnBorrow=true



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



  1. <!-- redis配置 -->
  2. <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  3. <property name="maxIdle" value="${redis.maxIdle}" />
  4. <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
  5. <property name="testOnBorrow" value="${redis.testOnBorrow}" />
  6. </bean>
  7. <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  8. p:host-name="${redis.host}" p:port="${redis.port}"
  9. p:password="${redis.password}" p:pool-config-ref="poolConfig" />
  10. <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
  11. <property name="connectionFactory" ref="connectionFactory" />
  12. </bean>


Redis 设置密码登录_java_06