前言
redis在生产环境中通常都会设置密码以保证一定的安全性,本篇blog就简单记录一下如何在redis中设置客户端登录密码。
修改redis.conf
RT,打开redis.conf文件,搜索requirepass关键字,如下图:
关注标记的那一行,#requirepass foobared。设置密码的方法就是去掉注释的#,把foobared替换成自己的密码即可,例如将密码设置为123456:
修改完成后重启redis,再次通过redis客户端redis-cli登录并操作可以发现会报一个身份认证错误:
这就说明我们已经成功的设置了密码,所以通过客户端连接的话必须加上密码参数才能正常连接:
如上图所示,加了-a参数之后即可正常连接并操作redis。
jedis设置密码
当我们用Java客户端连接redis时会遇到同样的问题,下面看一段简单的jedis连接redis的测试代码:
<span style="color:#333333"><code><span style="color:#000088"><span style="color:#000088">package</span></span> com.firstelite.test;
<span style="color:#000088"><span style="color:#000088">import</span></span> org.junit.Test;
<span style="color:#000088"><span style="color:#000088">import</span></span> redis.clients.jedis.Jedis;
<span style="color:#000088"><span style="color:#000088">public</span></span> <span style="color:#000088"><span style="color:#000088">class</span></span> <span style="color:#660066"><span style="color:#660066">Test4Jedis</span></span> {
<span style="color:#9b859d"><span style="color:#9b859d">@Test</span></span>
<span style="color:#000088"><span style="color:#000088">public</span></span> <span style="color:#000088"><span style="color:#000088">void</span></span> <span style="color:#333333">testTwo</span>() {
Jedis jedis = <span style="color:#000088"><span style="color:#000088">new</span></span> Jedis(<span style="color:#008800"><span style="color:#008800">"192.168.145.10"</span></span>);
System.out.println(<span style="color:#008800"><span style="color:#008800">"Connection to server sucessfully"</span></span>);
<span style="color:#880000"><span style="color:#880000">// 查看服务是否运行</span></span>
System.out.println(<span style="color:#008800"><span style="color:#008800">"Server is running: "</span></span> + jedis.ping());
}
}
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
非常简单,仅仅是测试一下Jedis是否连通redis服务器,运行junit后我们发现报异常了:
<span style="color:#333333"><code>redis.clients.jedis.exceptions.<span style="color:#333333">JedisDataException</span>: <span style="color:#333333">NOAUTH</span> <span style="color:#333333">Authentication</span> <span style="color:#333333">required</span>.
at redis<span style="color:#444444">.clients</span><span style="color:#444444">.jedis</span><span style="color:#444444">.<span style="color:#444444">Protocol</span></span><span style="color:#444444">.processError</span>(<span style="color:#333333">Protocol</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">117</span></span>)
at redis<span style="color:#444444">.clients</span><span style="color:#444444">.jedis</span><span style="color:#444444">.<span style="color:#444444">Protocol</span></span><span style="color:#444444">.process</span>(<span style="color:#333333">Protocol</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">142</span></span>)
at redis<span style="color:#444444">.clients</span><span style="color:#444444">.jedis</span><span style="color:#444444">.<span style="color:#444444">Protocol</span></span><span style="color:#444444">.read</span>(<span style="color:#333333">Protocol</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">196</span></span>)
at redis<span style="color:#444444">.clients</span><span style="color:#444444">.jedis</span><span style="color:#444444">.<span style="color:#444444">Connection</span></span><span style="color:#444444">.readProtocolWithCheckingBroken</span>(<span style="color:#333333">Connection</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">288</span></span>)
at redis<span style="color:#444444">.clients</span><span style="color:#444444">.jedis</span><span style="color:#444444">.<span style="color:#444444">Connection</span></span><span style="color:#444444">.getStatusCodeReply</span>(<span style="color:#333333">Connection</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">187</span></span>)
at redis<span style="color:#444444">.clients</span><span style="color:#444444">.jedis</span><span style="color:#444444">.<span style="color:#444444">BinaryJedis</span></span><span style="color:#444444">.ping</span>(<span style="color:#333333">BinaryJedis</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">109</span></span>)
at <span style="color:#000088">com</span><span style="color:#444444">.firstelite</span><span style="color:#444444">.test</span><span style="color:#444444">.<span style="color:#444444">Test</span></span><span style="color:#333333">4Jedis</span><span style="color:#444444">.testTwo</span>(<span style="color:#333333">Test4Jedis</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">15</span></span>)
at sun<span style="color:#444444">.<span style="color:#444444">reflect</span></span><span style="color:#444444">.<span style="color:#444444">NativeMethodAccessorImpl</span></span><span style="color:#444444">.invoke</span>0(<span style="color:#333333">Native</span> <span style="color:#333333">Method</span>)
at sun<span style="color:#444444">.<span style="color:#444444">reflect</span></span><span style="color:#444444">.<span style="color:#444444">NativeMethodAccessorImpl</span></span><span style="color:#444444">.invoke</span>(<span style="color:#333333">NativeMethodAccessorImpl</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">57</span></span>)
at sun<span style="color:#444444">.<span style="color:#444444">reflect</span></span><span style="color:#444444">.<span style="color:#444444">DelegatingMethodAccessorImpl</span></span><span style="color:#444444">.invoke</span>(<span style="color:#333333">DelegatingMethodAccessorImpl</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">43</span></span>)
at java<span style="color:#444444">.lang</span><span style="color:#444444">.<span style="color:#444444">reflect</span></span><span style="color:#444444">.<span style="color:#444444">Method</span></span><span style="color:#444444">.invoke</span>(<span style="color:#333333">Method</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">601</span></span>)
at org<span style="color:#444444">.junit</span><span style="color:#444444">.runners</span><span style="color:#444444">.model</span><span style="color:#444444">.<span style="color:#444444">FrameworkMethod</span></span>$<span style="color:#333333">1</span><span style="color:#444444">.runReflectiveCall</span>(<span style="color:#333333">FrameworkMethod</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">44</span></span>)
at org<span style="color:#444444">.junit</span><span style="color:#444444">.<span style="color:#444444">internal</span></span><span style="color:#444444">.runners</span><span style="color:#444444">.model</span><span style="color:#444444">.<span style="color:#444444">ReflectiveCallable</span></span><span style="color:#444444">.run</span>(<span style="color:#333333">ReflectiveCallable</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">15</span></span>)
at org<span style="color:#444444">.junit</span><span style="color:#444444">.runners</span><span style="color:#444444">.model</span><span style="color:#444444">.<span style="color:#444444">FrameworkMethod</span></span><span style="color:#444444">.invokeExplosively</span>(<span style="color:#333333">FrameworkMethod</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">41</span></span>)
at org<span style="color:#444444">.junit</span><span style="color:#444444">.<span style="color:#444444">internal</span></span><span style="color:#444444">.runners</span><span style="color:#444444">.statements</span><span style="color:#444444">.<span style="color:#444444">InvokeMethod</span></span><span style="color:#444444">.evaluate</span>(<span style="color:#333333">InvokeMethod</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">20</span></span>)
at org<span style="color:#444444">.junit</span><span style="color:#444444">.runners</span><span style="color:#444444">.<span style="color:#444444">BlockJUnit</span></span><span style="color:#333333">4ClassRunner</span><span style="color:#444444">.runNotIgnored</span>(<span style="color:#333333">BlockJUnit4ClassRunner</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">79</span></span>)
at org<span style="color:#444444">.junit</span><span style="color:#444444">.runners</span><span style="color:#444444">.<span style="color:#444444">BlockJUnit</span></span><span style="color:#333333">4ClassRunner</span><span style="color:#444444">.runChild</span>(<span style="color:#333333">BlockJUnit4ClassRunner</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">71</span></span>)
at org<span style="color:#444444">.junit</span><span style="color:#444444">.runners</span><span style="color:#444444">.<span style="color:#444444">BlockJUnit</span></span><span style="color:#333333">4ClassRunner</span><span style="color:#444444">.runChild</span>(<span style="color:#333333">BlockJUnit4ClassRunner</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">49</span></span>)
at org<span style="color:#444444">.junit</span><span style="color:#444444">.runners</span><span style="color:#444444">.<span style="color:#444444">ParentRunner</span></span>$<span style="color:#333333">3</span><span style="color:#444444">.run</span>(<span style="color:#333333">ParentRunner</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">193</span></span>)
at org<span style="color:#444444">.junit</span><span style="color:#444444">.runners</span><span style="color:#444444">.<span style="color:#444444">ParentRunner</span></span>$<span style="color:#333333">1</span><span style="color:#444444">.schedule</span>(<span style="color:#333333">ParentRunner</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">52</span></span>)
at org<span style="color:#444444">.junit</span><span style="color:#444444">.runners</span><span style="color:#444444">.<span style="color:#444444">ParentRunner</span></span><span style="color:#444444">.runChildren</span>(<span style="color:#333333">ParentRunner</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">191</span></span>)
at org<span style="color:#444444">.junit</span><span style="color:#444444">.runners</span><span style="color:#444444">.<span style="color:#444444">ParentRunner</span></span><span style="color:#444444">.access</span>$<span style="color:#333333">000</span>(<span style="color:#333333">ParentRunner</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">42</span></span>)
at org<span style="color:#444444">.junit</span><span style="color:#444444">.runners</span><span style="color:#444444">.<span style="color:#444444">ParentRunner</span></span>$<span style="color:#333333">2</span><span style="color:#444444">.evaluate</span>(<span style="color:#333333">ParentRunner</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">184</span></span>)
at org<span style="color:#444444">.junit</span><span style="color:#444444">.runners</span><span style="color:#444444">.<span style="color:#444444">ParentRunner</span></span><span style="color:#444444">.run</span>(<span style="color:#333333">ParentRunner</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">236</span></span>)
at org<span style="color:#444444">.eclipse</span><span style="color:#444444">.jdt</span><span style="color:#444444">.<span style="color:#444444">internal</span></span><span style="color:#444444">.junit</span>4<span style="color:#444444">.runner</span><span style="color:#444444">.<span style="color:#444444">JUnit</span></span><span style="color:#333333">4TestReference</span><span style="color:#444444">.run</span>(<span style="color:#333333">JUnit4TestReference</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">50</span></span>)
at org<span style="color:#444444">.eclipse</span><span style="color:#444444">.jdt</span><span style="color:#444444">.<span style="color:#444444">internal</span></span><span style="color:#444444">.junit</span><span style="color:#444444">.runner</span><span style="color:#444444">.<span style="color:#444444">TestExecution</span></span><span style="color:#444444">.run</span>(<span style="color:#333333">TestExecution</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">38</span></span>)
at org<span style="color:#444444">.eclipse</span><span style="color:#444444">.jdt</span><span style="color:#444444">.<span style="color:#444444">internal</span></span><span style="color:#444444">.junit</span><span style="color:#444444">.runner</span><span style="color:#444444">.<span style="color:#444444">RemoteTestRunner</span></span><span style="color:#444444">.runTests</span>(<span style="color:#333333">RemoteTestRunner</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">467</span></span>)
at org<span style="color:#444444">.eclipse</span><span style="color:#444444">.jdt</span><span style="color:#444444">.<span style="color:#444444">internal</span></span><span style="color:#444444">.junit</span><span style="color:#444444">.runner</span><span style="color:#444444">.<span style="color:#444444">RemoteTestRunner</span></span><span style="color:#444444">.runTests</span>(<span style="color:#333333">RemoteTestRunner</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">683</span></span>)
at org<span style="color:#444444">.eclipse</span><span style="color:#444444">.jdt</span><span style="color:#444444">.<span style="color:#444444">internal</span></span><span style="color:#444444">.junit</span><span style="color:#444444">.runner</span><span style="color:#444444">.<span style="color:#444444">RemoteTestRunner</span></span><span style="color:#444444">.run</span>(<span style="color:#333333">RemoteTestRunner</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">390</span></span>)
at org<span style="color:#444444">.eclipse</span><span style="color:#444444">.jdt</span><span style="color:#444444">.<span style="color:#444444">internal</span></span><span style="color:#444444">.junit</span><span style="color:#444444">.runner</span><span style="color:#444444">.<span style="color:#444444">RemoteTestRunner</span></span><span style="color:#444444">.main</span>(<span style="color:#333333">RemoteTestRunner</span><span style="color:#444444">.java</span>:<span style="color:#006666"><span style="color:#006666">197</span></span>)
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
显而易见,由于我们设置了密码但在这里又没有指定密码,所以报了和刚才相同的错误,那么如何指定密码呢?很简单,Jedis的父类BinaryJedis提供了这样一样方法:
<span style="color:#333333"><code> <span style="color:#000088"><span style="color:#000088">public</span></span> String <span style="color:#333333">auth</span>(<span style="color:#000088"><span style="color:#000088">final</span></span> String password) {
checkIsInMulti();
client.auth(password);
<span style="color:#000088"><span style="color:#000088">return</span></span> client.getStatusCodeReply();
}</code></span>
- 1
- 2
- 3
- 4
- 5
所以在创建了Jedis的实例后再加上一行jedis.auth("123456");
即可,最后看一下运行结果:
spring-data-redis设置密码
通常情况下在实际的java项目中我们会选择Spring提供的spring-data-redis来操作redis,spring的封装可以给我们提供很多便捷之处。那么spring-data-redis又是如何设置密码的呢?首先定义一个redis.properties配置文件,定义一组redis属性供spring加载使用,其中就包含密码(redis.password):
<span style="color:#333333"><code><span style="color:#444444"><span style="color:#444444"># Redis settings </span></span>
redis<span style="color:#444444">.host</span>=<span style="color:#006666"><span style="color:#006666">192.168</span>.145.10</span>
redis<span style="color:#444444">.port</span>=<span style="color:#006666"><span style="color:#006666">6379</span></span>
redis<span style="color:#444444">.password</span>=<span style="color:#006666"><span style="color:#006666">123456</span></span>
redis<span style="color:#444444">.timeout</span>=<span style="color:#006666"><span style="color:#006666">100000</span></span>
redis<span style="color:#444444">.maxTotal</span>=<span style="color:#006666"><span style="color:#006666">300</span></span>
redis<span style="color:#444444">.maxIdle</span>=<span style="color:#006666"><span style="color:#006666">100</span></span>
redis<span style="color:#444444">.maxWaitMillis</span>=<span style="color:#006666"><span style="color:#006666">1000</span></span>
redis<span style="color:#444444">.testOnBorrow</span>=<span style="color:#333333">true</span> </code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
然后在由Spring封装的JedisConnectionFactory中来设置密码属性即可,下面是完整redis配置:
<span style="color:#333333"><code><span style="color:#880000"><span style="color:#880000"><!-- redis配置 --></span></span>
<span style="color:#006666"><<span style="color:#000088"><span style="color:#000088">bean</span></span> <span style="color:#660066"><span style="color:#660066">id</span></span>=<span style="color:#008800"><span style="color:#008800">"poolConfig"</span></span> <span style="color:#660066"><span style="color:#660066">class</span></span>=<span style="color:#008800"><span style="color:#008800">"redis.clients.jedis.JedisPoolConfig"</span></span>></span>
<span style="color:#006666"><<span style="color:#000088"><span style="color:#000088">property</span></span> <span style="color:#660066"><span style="color:#660066">name</span></span>=<span style="color:#008800"><span style="color:#008800">"maxIdle"</span></span> <span style="color:#660066"><span style="color:#660066">value</span></span>=<span style="color:#008800"><span style="color:#008800">"${redis.maxIdle}"</span></span> /></span>
<span style="color:#006666"><<span style="color:#000088"><span style="color:#000088">property</span></span> <span style="color:#660066"><span style="color:#660066">name</span></span>=<span style="color:#008800"><span style="color:#008800">"maxWaitMillis"</span></span> <span style="color:#660066"><span style="color:#660066">value</span></span>=<span style="color:#008800"><span style="color:#008800">"${redis.maxWaitMillis}"</span></span> /></span>
<span style="color:#006666"><<span style="color:#000088"><span style="color:#000088">property</span></span> <span style="color:#660066"><span style="color:#660066">name</span></span>=<span style="color:#008800"><span style="color:#008800">"testOnBorrow"</span></span> <span style="color:#660066"><span style="color:#660066">value</span></span>=<span style="color:#008800"><span style="color:#008800">"${redis.testOnBorrow}"</span></span> /></span>
<span style="color:#006666"></<span style="color:#000088"><span style="color:#000088">bean</span></span>></span>
<span style="color:#006666"><<span style="color:#000088"><span style="color:#000088">bean</span></span> <span style="color:#660066"><span style="color:#660066">id</span></span>=<span style="color:#008800"><span style="color:#008800">"connectionFactory"</span></span> <span style="color:#660066"><span style="color:#660066">class</span></span>=<span style="color:#008800"><span style="color:#008800">"org.springframework.data.redis.connection.jedis.JedisConnectionFactory"</span></span>
<span style="color:#660066"><span style="color:#660066">p:host-name</span></span>=<span style="color:#008800"><span style="color:#008800">"${redis.host}"</span></span> <span style="color:#660066"><span style="color:#660066">p:port</span></span>=<span style="color:#008800"><span style="color:#008800">"${redis.port}"</span></span>
<span style="color:#660066"><span style="color:#660066">p:password</span></span>=<span style="color:#008800"><span style="color:#008800">"${redis.password}"</span></span> <span style="color:#660066"><span style="color:#660066">p:pool-config-ref</span></span>=<span style="color:#008800"><span style="color:#008800">"poolConfig"</span></span> /></span>
<span style="color:#006666"><<span style="color:#000088"><span style="color:#000088">bean</span></span> <span style="color:#660066"><span style="color:#660066">id</span></span>=<span style="color:#008800"><span style="color:#008800">"redisTemplate"</span></span> <span style="color:#660066"><span style="color:#660066">class</span></span>=<span style="color:#008800"><span style="color:#008800">"org.springframework.data.redis.core.StringRedisTemplate"</span></span>></span>
<span style="color:#006666"><<span style="color:#000088"><span style="color:#000088">property</span></span> <span style="color:#660066"><span style="color:#660066">name</span></span>=<span style="color:#008800"><span style="color:#008800">"connectionFactory"</span></span> <span style="color:#660066"><span style="color:#660066">ref</span></span>=<span style="color:#008800"><span style="color:#008800">"connectionFactory"</span></span> /></span>
<span style="color:#006666"></<span style="color:#000088"><span style="color:#000088">bean</span></span>></span></code></span>