redis在生產環境中一般都會設置密碼以保證必定的安全性,本篇blog就簡單記錄一下如何在redis中設置客戶端登陸密碼。java
說明:暫時沒有測試,通常本身的電腦安裝中使用沒有用到密碼。redis
打開redis.conf文件,搜索requirepass關鍵字,以下圖: spring
關注標記的那一行,#requirepass foobared。設置密碼的方法就是去掉註釋的#,把foobared替換成本身的密碼便可,例如將密碼設置爲123456: 安全
修改完成後重啓redis,再次經過redis客戶端redis-cli登陸並操做能夠發現會報一個身份認證錯誤: 服務器
這就說明咱們已經成功的設置了密碼,因此經過客戶端鏈接的話必須加上密碼參數才能正常鏈接: eclipse
如上圖所示,加了-a參數以後便可正常鏈接並操做redis。測試
當咱們用Java客戶端鏈接redis時會遇到一樣的問題,下面看一段簡單的jedis鏈接redis的測試代碼:ui
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後咱們發現報異常了:lua
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提供了這樣同樣方法:spa
因此在建立了Jedis的實例後再加上一行jedis.auth("123456");
便可,最後看一下運行結果:
一般狀況下在實際的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
boot2.#
spring.redis.host=192.168.81.129
spring.redis.port=6379
spring.redis.password=123456
而後在由Spring封裝的JedisConnectionFactory中來設置密碼屬性便可,下面是完整redis配置:
<!-- redis配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxWaitMillis" value="${redis.maxWaitMillis}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.password}" p:pool-config-ref="poolConfig" /> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean>
</bean
出處: https://blog.csdn.net/crazy__qu/article/details/78738264