Skip to content

Commit 202a426

Browse files
mourezwellsnicoll
authored andcommitted
Add support for Redis sentinel username
See gh-29661
1 parent ebbd167 commit 202a426

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ protected final RedisSentinelConfiguration getSentinelConfig() {
9999
if (this.properties.getPassword() != null) {
100100
config.setPassword(RedisPassword.of(this.properties.getPassword()));
101101
}
102+
config.setSentinelUsername(sentinelProperties.getUsername());
102103
if (sentinelProperties.getPassword() != null) {
103104
config.setSentinelPassword(RedisPassword.of(sentinelProperties.getPassword()));
104105
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,11 @@ public static class Sentinel {
377377
*/
378378
private String password;
379379

380+
/**
381+
* Login username for authenticating with sentinel(s).
382+
*/
383+
private String username;
384+
380385
public String getMaster() {
381386
return this.master;
382387
}
@@ -401,6 +406,14 @@ public void setPassword(String password) {
401406
this.password = password;
402407
}
403408

409+
public String getUsername() {
410+
return this.username;
411+
}
412+
413+
public void setUsername(String username) {
414+
this.username = username;
415+
}
416+
404417
}
405418

406419
/**

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,25 @@ void testRedisConfigurationWithSentinelPasswordAndDataNodePassword() {
315315
assertThat(getUserName(connectionFactory)).isNull();
316316
assertThat(connectionFactory.getPassword()).isEqualTo("password");
317317
RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration();
318+
assertThat(sentinelConfiguration.getSentinelUsername()).isNull();
319+
assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret");
320+
Set<RedisNode> sentinels = sentinelConfiguration.getSentinels();
321+
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))
322+
.contains("127.0.0.1:26379", "127.0.0.1:26380");
323+
});
324+
}
325+
326+
@Test
327+
void testRedisConfigurationWithSentinelAuthenticationAndDataNodeAuthentication() {
328+
this.contextRunner.withPropertyValues("spring.redis.username=username", "spring.redis.password=password",
329+
"spring.redis.sentinel.username=sentinel", "spring.redis.sentinel.password=secret",
330+
"spring.redis.sentinel.master:mymaster",
331+
"spring.redis.sentinel.nodes:127.0.0.1:26379, 127.0.0.1:26380").run((context) -> {
332+
LettuceConnectionFactory connectionFactory = context.getBean(LettuceConnectionFactory.class);
333+
assertThat(getUserName(connectionFactory)).isEqualTo("username");
334+
assertThat(connectionFactory.getPassword()).isEqualTo("password");
335+
RedisSentinelConfiguration sentinelConfiguration = connectionFactory.getSentinelConfiguration();
336+
assertThat(sentinelConfiguration.getSentinelUsername()).isEqualTo("sentinel");
318337
assertThat(new String(sentinelConfiguration.getSentinelPassword().get())).isEqualTo("secret");
319338
Set<RedisNode> sentinels = sentinelConfiguration.getSentinels();
320339
assertThat(sentinels.stream().map(Object::toString).collect(Collectors.toSet()))

0 commit comments

Comments
 (0)