Spring2 转到 Spring3 后整合 Redis 报错 NOAUTH Authentication required
我在spring boot 2.7.3
中使用SpringDataRedis
正常运行。
但在spring boot 3.4.0
中使用就会报错NOAUTH Authentication required.
Redis版本: 3.2.100-windows版
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置类:
@Configuration
public class RedisConfiguration {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate<>();
// 设置 Redis 的连接工厂对象
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 设置 Redis key 的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
spring boot 3.4.0 的 application (相较于2.7.3增加了data
):
# Redis 主机地址
spring.data.redis.host=localhost
# Redis 端口号
spring.data.redis.port=6379
# Redis 密码
spring.data.redis.password=123456
# Redis 指定数据库
spring.data.redis.database=1
在redis.windows.conf
中将密码注释,并注释 application 中的spring.data.redis.password=123456
后,能够成功运行。
猜测是因为 spring 版本太高而 redis 版本太低的原因?
redis 3.0 版本仅支持 RESP2 的通信协议,而 Lettuce 6.x 版本开始,使用 RESP3(Redis 6.x 引入)的 HELLO 命令进行版本自适应判断,但是对于不支持 HELLO 命令的低版本实例,兼容性存在一定问题。刚好 springboot 版本默认的引入的 redis 客户端是 lettuce 6.x ,协议不通。