1. 程式人生 > >Redis 設定密碼登入 轉

Redis 設定密碼登入 轉

前言

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>