Polishing.

Reformat code. Consider sentinel username in JedisConnectionFactory. Update years in license headers.

See #2218
Original pull request: #2224.
This commit is contained in:
Mark Paluch
2022-01-07 11:04:58 +01:00
parent f333b184a5
commit d0f17b167e
10 changed files with 95 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2021 the original author or authors.
* Copyright 2018-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -400,6 +400,23 @@ public interface RedisConfiguration {
return getPassword();
}
/**
* Create and set a username with the given {@link String}. Requires Redis 6 or newer.
*
* @param sentinelUsername the username for sentinel.
* @since 2.7
*/
void setSentinelUsername(@Nullable String sentinelUsername);
/**
* Get the username to use when connecting.
*
* @return {@literal null} if none set.
* @since 2.7
*/
@Nullable
String getSentinelUsername();
/**
* Create and set a {@link RedisPassword} to be used when authenticating with Redis Sentinel from the given
* {@link String}.
@@ -440,20 +457,6 @@ public interface RedisConfiguration {
*/
RedisPassword getSentinelPassword();
/**
* Create and set a username with the given {@link String}. Requires Redis 6 or newer.
*
* @param sentinelUsername the username for sentinel.
*/
void setSentinelUsername(@Nullable String sentinelUsername);
/**
* Get the username to use when connecting.
*
* @return {@literal null} if none set.
*/
@Nullable
String getSentinelUsername();
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -46,8 +46,8 @@ public class RedisSentinelConfiguration implements RedisConfiguration, SentinelC
private static final String REDIS_SENTINEL_MASTER_CONFIG_PROPERTY = "spring.redis.sentinel.master";
private static final String REDIS_SENTINEL_NODES_CONFIG_PROPERTY = "spring.redis.sentinel.nodes";
private static final String REDIS_SENTINEL_PASSWORD_CONFIG_PROPERTY = "spring.redis.sentinel.password";
private static final String REDIS_SENTINEL_USERNAME_CONFIG_PROPERTY = "spring.redis.sentinel.username";
private static final String REDIS_SENTINEL_PASSWORD_CONFIG_PROPERTY = "spring.redis.sentinel.password";
private @Nullable NamedNode master;
private Set<RedisNode> sentinels;
@@ -343,6 +343,9 @@ public class RedisSentinelConfiguration implements RedisConfiguration, SentinelC
if (!ObjectUtils.nullSafeEquals(dataNodePassword, that.dataNodePassword)) {
return false;
}
if (!ObjectUtils.nullSafeEquals(sentinelUsername, that.sentinelUsername)) {
return false;
}
return ObjectUtils.nullSafeEquals(sentinelPassword, that.sentinelPassword);
}
@@ -357,6 +360,7 @@ public class RedisSentinelConfiguration implements RedisConfiguration, SentinelC
result = 31 * result + database;
result = 31 * result + ObjectUtils.nullSafeHashCode(dataNodeUsername);
result = 31 * result + ObjectUtils.nullSafeHashCode(dataNodePassword);
result = 31 * result + ObjectUtils.nullSafeHashCode(sentinelUsername);
result = 31 * result + ObjectUtils.nullSafeHashCode(sentinelPassword);
return result;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2021 the original author or authors.
* Copyright 2011-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -360,8 +360,9 @@ public class JedisConnectionFactory implements InitializingBean, DisposableBean,
this.initialized = true;
}
private JedisClientConfig createSentinelClientConfig(SentinelConfiguration sentinelConfiguration) {
return createClientConfig(0, null, sentinelConfiguration.getSentinelPassword());
JedisClientConfig createSentinelClientConfig(SentinelConfiguration sentinelConfiguration) {
return createClientConfig(0, sentinelConfiguration.getSentinelUsername(),
sentinelConfiguration.getSentinelPassword());
}
private JedisClientConfig createClientConfig(int database, @Nullable String username, RedisPassword password) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2021 the original author or authors.
* Copyright 2013-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -516,7 +516,7 @@ public abstract class LettuceConverters extends Converters {
String sentinelUsername = sentinelConfiguration.getSentinelUsername();
if (StringUtils.hasText(sentinelUsername) && sentinelPassword.isPresent()) {
// See https://github.com/lettuce-io/lettuce-core/issues/1404
sentinelBuilder.withAuthentication(sentinelUsername, new String(sentinelPassword.toOptional().orElse((new char[0]))));
sentinelBuilder.withAuthentication(sentinelUsername, sentinelPassword.get());
} else {
sentinelPassword.toOptional().ifPresent(sentinelBuilder::withPassword);
}
@@ -527,9 +527,9 @@ public abstract class LettuceConverters extends Converters {
String username = sentinelConfiguration.getUsername();
RedisPassword password = sentinelConfiguration.getPassword();
if (StringUtils.hasText(username)) {
if (StringUtils.hasText(username) && password.isPresent()) {
// See https://github.com/lettuce-io/lettuce-core/issues/1404
builder.withAuthentication(username, new String(password.toOptional().orElse(new char[0])));
builder.withAuthentication(username, password.get());
} else {
password.toOptional().ifPresent(builder::withPassword);
}