Spring Boot集成Redis并设置密码后报错: NOAUTH Authentication required

报错信息:

bash 复制代码
io.lettuce.core.RedisCommandExecutionException: NOAUTH Authentication required.

Redis密码配置确认无误,但是只要使用Redis存储就报这个异常。很可能是因为配置的spring.redis.password没有被读取到。



基本依赖:

bash 复制代码
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

基本设置(application.properties):

bash 复制代码
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=your_password
spring.redis.database=0
spring.redis.timeout=3000


解决方法:

java 复制代码
@Configuration
public class RedisConfig {

    @Value("${spring.redis.password}")
    private String redisPassword;
    @Value("${spring.redis.host}")
    private String redisHost;
    @Value("${spring.redis.port}")
    private int redisPort;

    /**
     * 解决redis认证报错问题:io.lettuce.core.RedisCommandExecutionException: NOAUTH Authentication required.
     *
     * @return
     */
    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory(
                new RedisStandaloneConfiguration(redisHost, redisPort) {{
                    setPassword(RedisPassword.of(redisPassword));
                }});
    }
}
相关推荐
Flittly14 小时前
【AgentScope Java新手村系列】(16)从RAG到多路检索
java·spring boot·spring
人活一口气19 小时前
从JVM调优到MCP协议:Java全栈技术体系深度总结与企业级架构实践
java·spring boot
Java陈序员2 天前
企业级!一个基于 Java 开发的开源 AI 应用开发平台!
spring boot·agent·mcp
杨运交2 天前
[041][公共模块]分布式唯一ID生成器设计与实现:一款灵活可扩展的雪花算法框架
spring boot
用户3074596982073 天前
Redis 延时队列详解
redis
烤代码的吐司君3 天前
Redis 数据结构 ZSet, BIT, HyperLogLog,Geo 空间数据
redis·后端
Flittly3 天前
【AgentScope Java新手村系列】(14)人机交互
java·spring boot·spring
Flynt4 天前
从Spring Boot 4.0升到4.1,我在Maven和gRPC上栽了跟头
java·spring boot·后端
掉鱼的猫5 天前
Spring Boot → Solon 注解迁移实战指南:一张对照表说清楚
java·spring boot
leeyi5 天前
Checkpoint 机制:Agent 怎么在断电后接着跑
redis·aigc·agent