登录  /  注册
首页 > 数据库 > Redis > 正文

在redis中设置客户端登录密码

王林
发布: 2021-01-01 12:16:16
转载
4857人浏览过

在redis中设置客户端登录密码

导语:

为了保证安全性,redis在生产环境中一般都会设置登录密码,今天我就来为大家介绍一下如何设置登录密码。

(学习视频分享:redis视频教程

修改redis.conf

RT,打开redis.conf文件,搜索requirepass关键字,如下图: 

7df68216d8f04ef100b67340a79c54b.png

关注标记的那一行,#requirepass foobared。设置密码的方法就是去掉注释的#,把foobared替换成自己的密码即可,例如将密码设置为123456:

dc0cd1e3e212e721233db1f90bbbc54.png

修改完成后重启redis,再次通过redis客户端redis-cli登录并操作可以发现会报一个身份认证错误: 

adfc9b11cf8701c23bd280ec8fd7fb7.png

这就说明我们已经成功的设置了密码,所以通过客户端连接的话必须加上密码参数才能正常连接: 

0c689cdd75071e4c4c5d29fb7e00c5d.png

如上图所示,加了-a参数之后即可正常连接并操作redis。

jedis设置密码

当我们用Java客户端连接redis时会遇到同样的问题,下面看一段简单的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");
        // 查看服务是否运行
        System.out.println("Server is running: " + jedis.ping());
    }
 
}
登录后复制

非常简单,仅仅是测试一下Jedis是否连通redis服务器,运行junit后我们发现报异常了:

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提供了这样一样方法:

  public String auth(final String password) {
    checkIsInMulti();
    client.auth(password);
    return client.getStatusCodeReply();
  }
登录后复制

所以在创建了Jedis的实例后再加上一行jedis.auth("123456"); 即可,最后看一下运行结果:

2ac9ed6a09528eafdd188eb991183f1.png

spring-data-redis设置密码

通常情况下在实际的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
登录后复制

然后在由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>
登录后复制

相关推荐:redis数据库教程

以上就是在redis中设置客户端登录密码的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:csdn网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号