欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          在redis中設(shè)置客戶端登錄密碼

          在redis中設(shè)置客戶端登錄密碼

          導(dǎo)語:

          為了保證安全性,redis在生產(chǎn)環(huán)境中一般都會(huì)設(shè)置登錄密碼,今天我就來為大家介紹一下如何設(shè)置登錄密碼。

          (學(xué)習(xí)視頻分享:redis視頻教程)

          修改redis.conf

          RT,打開redis.conf文件,搜索requirepass關(guān)鍵字,如下圖:

          在redis中設(shè)置客戶端登錄密碼

          關(guān)注標(biāo)記的那一行,#requirepass foobared。設(shè)置密碼的方法就是去掉注釋的#,把foobared替換成自己的密碼即可,例如將密碼設(shè)置為123456:

          在redis中設(shè)置客戶端登錄密碼

          修改完成后重啟redis,再次通過redis客戶端redis-cli登錄并操作可以發(fā)現(xiàn)會(huì)報(bào)一個(gè)身份認(rèn)證錯(cuò)誤:

          在redis中設(shè)置客戶端登錄密碼

          這就說明我們已經(jīng)成功的設(shè)置了密碼,所以通過客戶端連接的話必須加上密碼參數(shù)才能正常連接:

          在redis中設(shè)置客戶端登錄密碼

          如上圖所示,加了-a參數(shù)之后即可正常連接并操作redis。

          jedis設(shè)置密碼

          當(dāng)我們用Java客戶端連接redis時(shí)會(huì)遇到同樣的問題,下面看一段簡單的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");         // 查看服務(wù)是否運(yùn)行         System.out.println("Server is running: " + jedis.ping());     }   }

          非常簡單,僅僅是測試一下Jedis是否連通redis服務(wù)器,運(yùn)行junit后我們發(fā)現(xiàn)報(bào)異常了:

          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)

          顯而易見,由于我們設(shè)置了密碼但在這里又沒有指定密碼,所以報(bào)了和剛才相同的錯(cuò)誤,那么如何指定密碼呢?很簡單,Jedis的父類BinaryJedis提供了這樣一樣方法:

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

          所以在創(chuàng)建了Jedis的實(shí)例后再加上一行jedis.auth("123456"); 即可,最后看一下運(yùn)行結(jié)果:

          在redis中設(shè)置客戶端登錄密碼

          spring-data-redis設(shè)置密碼

          通常情況下在實(shí)際的java項(xiàng)目中我們會(huì)選擇Spring提供的spring-data-redis來操作redis,spring的封裝可以給我們提供很多便捷之處。那么spring-data-redis又是如何設(shè)置密碼的呢?首先定義一個(gè)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中來設(shè)置密碼屬性即可,下面是完整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>

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)