Why am I getting "NOAUTH Authentication required" with Spring Boot?
Asked Answered
A

3

5

I have a simple redis server running with a password. I want to talk with it from my spring boot app. I look here and see there is a spring.redis.password so my application-local.yml (running with -Dspring.profiles.active=local) looks like...

spring:
  redis:
    password: ... 

but when I run I get

NOAUTH Authentication required

What am I missing? I can connect via node.js like...

import redis from "redis";

const client = redis.createClient({
 password: "..."
});

Additional Code

@Bean
LettuceConnectionFactory redisConnectionFactory(){
    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
    return new LettuceConnectionFactory(config);
}

@Bean
public RedisTemplate<String, Object> redisTemplate(){
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory());
    return template;
}

also tried...

@Bean
LettuceConnectionFactory redisConnectionFactory(){
    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
    if (redisPassword != null){
        config.setPassword(redisPassword);
    }
    return new LettuceConnectionFactory(config);
}

and this works but seems overly verbose given it is a standard property.

Alanalana answered 10/11, 2020 at 19:4 Comment(0)
T
4

If you need less verbose configuration, you can remove RedisConnectionFactory bean from your configuration code and just inject the RedisConnectionFactory bean in your redisTemplate. redisConnectionFactory will be populated with properties from application.yml:

@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}

In this case, Spring injects LettuceConnectionFactory by default.

The problem was here: new RedisStandaloneConfiguration(). If you look at the code of constructor you will see that empty password is created and there is no way to set it besides calling a setter.


Old answer: You need to fetch your data from application.yml through RedisProperties class. Try this:

@Bean
RedisConnectionFactory redisConnectionFactory(RedisProperties props) {
    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();

    config.setPassword(props.getPassword());

    return new LettuceConnectionFactory(config);
}

@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}

Here props contains properties from spring.redis section

Tableware answered 10/11, 2020 at 19:35 Comment(6)
Right but this seems verbose why isn't it auto configured if it is a default property? For example I don't have to set the host to 127.0.0.1Alanalana
Also this answer in general is overly verbose see my version it doesn't require anything but password to be set.Alanalana
Host and port are auto-configured, I put them just in case you need to launch your service with redis not on localhost:6379. Password should be set manuallyTableware
Updated my answer with less verbose configTableware
Now the updated answer doesn't include the section for the password rt? That was the solution to the original questionAlanalana
It does not include the section for password. The answer to original question is remove RedisConnectionFactory bean and let Spring create a one for you. Created RedisConnectionFactory is populated with properties from application.ymlTableware
G
2

My app was running alright with Spring Boot v2.2.4.RELEASE. After migrating to Spring v3.1.1, the same problem occured as author mentions.

I tried several approaches including the answer proposed by nkrivenko which worked. However, here is the solution that I found afterwards and finally accept as correct:

In application.yaml (.property) file I previously had:

spring:
  redis:
    client-name: JEDIS
    timeout: 3000
    port: ${REDIS_PORT:6379}
    host: ${REDIS_HOSTNAME:localhost}
    password: ${REDIS_PASSWORD:theRedisPasswordHere}

Thanks to IntelliJ IDEA that suggested this structure is deprecated. As it recommends just add data: line between spring: and redis: lines. Now it looks like and works without any code change:

spring:
  data:
    redis:
      client-name: JEDIS
      timeout: 3000
      port: ${REDIS_PORT:6379}
      host: ${REDIS_HOSTNAME:localhost}
      password: ${REDIS_PASSWORD:theRedisPasswordHere}

Edited: thanks to Mustafa Güven's comment

Grundyism answered 18/7, 2023 at 7:28 Comment(1)
adding data: between spring: and redis: in yaml solved the problem.Darksome
L
0

for me, problem because version of spring and redis (2.1.4), after upgrade newer version, it work normal Please use with:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.9.RELEASE</version>
</parent>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.3.9.RELEASE</version>
</dependency>

I use redis sentinel and my properties is:

spring.redis.database=0
spring.redis.sentinel.master=
spring.redis.sentinel.nodes=
spring.redis.sentinel.password=
spring.redis.password=
spring.redis.timeout=60000
Lheureux answered 5/11, 2022 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.