Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required
作者:mmseoamin日期:2023-12-18

1. 背景

Spring Boot版本升级为:2.6.14
redis依赖:


  org.springframework.boot
  spring-boot-starter-data-redis

redis配置不变,还是带password的:
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第1张
项目启动后,获取redis连接时,报错:NOAUTH Authentication required
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第2张

2. 问题分析

spring-boot-starer-data-redis支持使用Jedis和Lettuce作为redis客户端,如果配置不指定则默认使用Lettuce。
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第3张
不管是Lettuce还是还是Jedis,核心是构建RedisConnectionFactory。
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第4张
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第5张
不管是Lettuce还是Jedis,都支持单节点、哨兵模式和集群模式的Redis服务端,并且对应的配置都会设置上Password,证明和配置和代码没关系。
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第6张
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第7张
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第8张
所以说,源码和配置都是没问题的,只要配置了密码,就一定会设置到客户端里面。

3. 问题所在

那是什么原因?初步猜测是redis客户端和服务端版本上的一些支持问题。
看看Spring Boot2.6.14的Lettuce版本基本是保持在比较新的了:
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第9张
再看看我们的redis服务端的版本:
我的天,现在redis都出到6了吧。。
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第10张
现在这么一看,最快的解决方法应该是降级Lettuce。
我们试试降级到5.x
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第11张
项目启动,这次不是认证问题,而是找不到一些常量,此版本的spring-boot-starter-data-redis和5.x的Lettuce并不兼容,所以说,不能只是降低Lettuce版本,要不就直接降低spring-boot-starter-data-redis的版本,但是既然升级到新版本的SpringBoot,肯定就是想使用新的特性和支持,不可能又单独将某个组件降级回去吧。。
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第12张
没啥头绪,直接到官网看看Lettuce的版本介绍吧。
看了最近几个版本的介绍,有和Redis版本直接相关的是Lettuce6.0,其中介绍支持Redis6的RESP3协议
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第13张
到源码里面搜,还真的搜出点东西:
意思是说,到Lettuce6.0后,将使用RESP3协议,而RESP3协议是Redis6.0才支持的,那么我们的Redis服务端版本才2.8.x,肯定是废掉的了。
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第14张
所以解决方法基本还是围绕Lettuce的版本解决,要不就是将spring-boot-starter-redis进行降级(不考虑),将对应的Lettuce版本也降到5.2以下,要不就是将RESP3协议改为使用RESP2协议。
那我们肯定是选择后者的,因为Lettuce6.0版本也提到,同时支持RESP2和RESP3。
至于怎么修改RESP协议,这里直接上代码。
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第15张
项目启动成功,并操作Redis成功:
Spring Boot Starter Data Redis使用Lettuce客户端报错:NOAUTH Authentication required,image.png,第16张