In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to set the client login password in redis. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Introduction:
To ensure security, redis generally sets a login password in a production environment. Today I'm going to show you how to set a login password.
Modify redis.conf
RT, open the redis.conf file and search for the requirepass keyword, as shown below:
Follow the line of the tag, # requirepass foobared. The way to set a password is to remove the comment # and replace foobared with your own password, for example, set the password to 123456:
Restart redis after the modification is completed, log in again through the redis client redis-cli and operate, you will find that an authentication error will be reported:
This means that we have successfully set the password, so if you connect through the client, you must add a password parameter to connect normally:
As shown in the figure above, the redis can be connected and operated normally with the-a parameter added.
Jedis set password
We will encounter the same problem when we use the Java client to connect to redis. Let's take a look at a simple test code for connecting jedis to 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"); / / check whether the service is running System.out.println ("Server is running:" + jedis.ping ());}}
It's very simple. Just test whether Jedis is connected to the redis server. After running junit, we find that an exception has been reported:
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 (NativeMethod) 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: ) 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)
Obviously, because we set the password but do not specify the password here, so we reported the same error as before, so how to specify the password? Quite simply, BinaryJedis, the parent class of Jedis, provides this method:
Public String auth (final String password) {checkIsInMulti (); client.auth (password); return client.getStatusCodeReply ();}
So after creating an instance of Jedis, add a line of jedis.auth ("123456");. Finally, take a look at the running result:
Spring-data-redis set password
Usually in the actual java project, we will choose the spring-data-redis provided by Spring to operate the encapsulation of redis,spring, which can provide us with a lot of convenience. So how does spring-data-redis set the password? First, define a redis.properties configuration file and define a set of redis properties for spring to load, including the redis.password:
# Redis settings redis.host=192.168.145.10 redis.port=6379 redis.password=123456redis.timeout=100000 redis.maxTotal=300 redis.maxIdle=100 redis.maxWaitMillis=1000 redis.testOnBorrow=true
Then set the password properties in the JedisConnectionFactory encapsulated by Spring. Here is the complete redis configuration:
Thank you for reading! This is the end of this article on "how to set the client login password in redis". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.