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

发布于:2025-03-23 ⋅ 阅读:(28) ⋅ 点赞:(0)

报错信息:

io.lettuce.core.RedisCommandExecutionException: NOAUTH Authentication required.

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



基本依赖:

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):

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=your_password
spring.redis.database=0
spring.redis.timeout=3000


解决方法:

@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));
                }});
    }
}