解决SpringBoot环境下Redis哨兵模式连接失败问题,“NOAUTH Authentication required”

io.lettuce.core.RedisCommandException Exception:“NOAUTH Authentication required”

在某行工作,项目上线代码,uat环境无异常,上到pp环境有问题,报redis连接不上;

观察配置,发觉是apollo的配置是哨兵模式,有个哨兵密码。spring2.2.6RELEASE版本问题。于是写了全局配置,读取配置中的sentinel.password

package com.yupi.springbootinit.config;

import cn.hutool.core.lang.Assert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Configuration
public class RedisConfig {

    // 配置sentinel密码不生效,提示NOAUTH Authentication required。
//https://github.com/lettuce-io/lettuce-core/issues/1543
    @Bean
    public RedisSentinelConfiguration redisSentinelConfiguration(RedisProperties properties , @Value("${spring.redis.sentinel.password}") String sentinelPassword){
        RedisProperties.Sentinel sentinelProperties = properties.getSentinel();

        if (sentinelProperties != null) {
            RedisSentinelConfiguration config = new RedisSentinelConfiguration();
            config.master(sentinelProperties.getMaster());
            config.setSentinels(createSentinels(sentinelProperties));

            if (properties.getPassword() != null){
                config.setPassword(RedisPassword.of(properties.getPassword()));
            }
            if (sentinelPassword != null) {
            config.setSentinelPassword(RedisPassword.of(sentinelPassword));
            }
            config.setDatabase(properties.getDatabase());
            return config;
        }
        return null;
    }
        private List<RedisNode> createSentinels(RedisProperties.Sentinel sentinel) {
            List<RedisNode> nodes = new ArrayList<>();
            for (String node : sentinel.getNodes()) {
                String[] split = StringUtils.split(node, ":");
                Assert.notNull(split,"Must be defined as 'host:port'");
                Assert.state(split.length==2,"Must be defined as 'host:port'");
                nodes.add(new RedisNode(split[0],Integer.parseInt(split[1])));
            }
           return nodes;
        }

}

### Redis 连接失败 NOAUTH Authentication Required解决方案 当尝试连接Redis 实例并收到 `NOAUTH Authentication required` 错误时,这表明客户端未提供必要的认证凭证。为了成功建立连接,需确保已配置正确的身份验证机制。 #### 方法一:通过命令行工具 redis-cli 认证 如果使用的是命令行工具来访问 Redis,则可以在启动 cli 后输入如下指令完成身份验证: ```bash 127.0.0.1:6379> AUTH your_password_here OK ``` 此处的 `your_password_here` 应替换为实际设置过的密码[^2]。 #### 方法二:修改配置文件以启用密码保护 对于希望长期生效的身份验证措施,在服务器端编辑配置文件是一种有效方式。具体操作是在 Redis 配置文件(通常是 `redis.conf` 或者特定平台下的变体如 Windows 版本中的 `.windows-service.conf` 文件)中添加或更新以下参数: ```properties requirepass your_secure_password ``` 保存更改后的配置文件,并重启 Redis 服务使新设置生效[^3]。 #### 方法三:编程接口实现自动登录 针对应用程序级别的集成场景,可以利用支持库提供的 API 来处理认证过程。例如采用 Lettuce 客户端库时,可以通过设定 URI 方式指定用户名和密码: ```java RedisURI.builder() .withHost("localhost") .withPort(6379) .withPassword("your_secure_password".toCharArray()) .build(); ``` 上述代码片段展示了如何构建带有认证信息的目标地址对象,进而创建经过鉴权的安全会话[^1]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值