You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3.1.1升级后Redis连接报NOAUTH错误求助

Spring Boot 3.1.1 连接Redis 6.2.6 认证失败问题解决

问题重现

升级Spring Boot到3.1.1版本后,使用以下Reactive Redis配置启动应用时,触发Redis连接失败异常:

@Bean
public ReactiveRedisConnectionFactory redisFirstConnectionFactory() {
    var redisConf = new RedisStandaloneConfiguration();
    redisConf.setHostName(host);
    redisConf.setPort(port);
    redisConf.setPassword(password);
    redisConf.setDatabase(0);
    return new LettuceConnectionFactory(redisConf,
            LettuceClientConfiguration
                    .builder()
                    .commandTimeout(Duration.ofSeconds(10))
                    .build());
}

@Bean
public ReactiveStringRedisTemplate redisTemplate(ReactiveRedisConnectionFactory cf) {
    return new ReactiveStringRedisTemplate(cf);
}

启动时抛出核心异常:

Caused by: io.lettuce.core.RedisCommandExecutionException: NOAUTH HELLO must be called with the client already authenticated, otherwise the HELLO AUTH <user> <pass> option can be used to authenticate the client and select the RESP protocol version at the same time

注:Redis版本为6.2.6,使用RedisInsight可正常通过相同密码连接。

问题根源

Redis 6.x引入了RESP3协议与HELLO命令,Spring Boot 3.1.x配套的Lettuce客户端默认会先发送HELLO命令协商协议版本,但如果Redis实例开启了密码认证,未携带凭证的HELLO命令会被拒绝,触发上述NOAUTH错误。而RedisInsight的客户端逻辑会先执行AUTH认证再进行协议协商,因此可以正常连接。

解决方案

方案1:强制使用RESP2协议(快速解决)

修改Lettuce客户端配置,指定使用RESP2协议,避免触发HELLO命令的认证冲突:

@Bean
public ReactiveRedisConnectionFactory redisFirstConnectionFactory() {
    var redisConf = new RedisStandaloneConfiguration();
    redisConf.setHostName(host);
    redisConf.setPort(port);
    redisConf.setPassword(RedisPassword.of(password)); // 用RedisPassword规范封装密码
    redisConf.setDatabase(0);

    var lettuceConfig = LettuceClientConfiguration.builder()
            .commandTimeout(Duration.ofSeconds(10))
            .protocolVersion(ProtocolVersion.RESP2) // 强制使用RESP2协议
            .build();

    return new LettuceConnectionFactory(redisConf, lettuceConfig);
}

方案2:使用Spring Boot自动配置属性(推荐)

放弃手动配置ReactiveRedisConnectionFactory,改用Spring Boot自动配置,在application.yml或application.properties中配置:

spring:
  redis:
    host: ${REDIS_HOST}
    port: ${REDIS_PORT}
    password: ${REDIS_PASSWORD}
    database: 0
    lettuce:
      client:
        timeout: 10s
      protocol: RESP2 # 指定RESP2协议,避免认证冲突

自动配置会自动处理Lettuce与Redis 6.x的认证交互逻辑,减少手动配置出错概率。

验证

修改配置后重启应用,通过Actuator健康端点(/actuator/health)或编写简单的Redis操作测试用例,确认Redis连接正常。

内容的提问来源于stack exchange,提问作者Hatef Alipoor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 01:20:18