Add support for Redis 6 authentication with username

Closes gh-22702
This commit is contained in:
Stephane Nicoll
2020-09-29 09:59:32 +02:00
parent a5c20a5132
commit 36382e599c
4 changed files with 79 additions and 25 deletions

View File

@@ -60,11 +60,13 @@ abstract class RedisConnectionConfiguration {
ConnectionInfo connectionInfo = parseUrl(this.properties.getUrl());
config.setHostName(connectionInfo.getHostName());
config.setPort(connectionInfo.getPort());
config.setUsername(connectionInfo.getUsername());
config.setPassword(RedisPassword.of(connectionInfo.getPassword()));
}
else {
config.setHostName(this.properties.getHost());
config.setPort(this.properties.getPort());
config.setUsername(this.properties.getUsername());
config.setPassword(RedisPassword.of(this.properties.getPassword()));
}
config.setDatabase(this.properties.getDatabase());
@@ -80,6 +82,7 @@ abstract class RedisConnectionConfiguration {
RedisSentinelConfiguration config = new RedisSentinelConfiguration();
config.master(sentinelProperties.getMaster());
config.setSentinels(createSentinels(sentinelProperties));
config.setUsername(this.properties.getUsername());
if (this.properties.getPassword() != null) {
config.setPassword(RedisPassword.of(this.properties.getPassword()));
}
@@ -108,6 +111,7 @@ abstract class RedisConnectionConfiguration {
if (clusterProperties.getMaxRedirects() != null) {
config.setMaxRedirects(clusterProperties.getMaxRedirects());
}
config.setUsername(this.properties.getUsername());
if (this.properties.getPassword() != null) {
config.setPassword(RedisPassword.of(this.properties.getPassword()));
}
@@ -141,15 +145,20 @@ abstract class RedisConnectionConfiguration {
throw new RedisUrlSyntaxException(url);
}
boolean useSsl = ("rediss".equals(scheme));
String username = null;
String password = null;
if (uri.getUserInfo() != null) {
password = uri.getUserInfo();
int index = password.indexOf(':');
String candidate = uri.getUserInfo();
int index = candidate.indexOf(':');
if (index >= 0) {
password = password.substring(index + 1);
username = candidate.substring(0, index);
password = candidate.substring(index + 1);
}
else {
password = candidate;
}
}
return new ConnectionInfo(uri, useSsl, password);
return new ConnectionInfo(uri, useSsl, username, password);
}
catch (URISyntaxException ex) {
throw new RedisUrlSyntaxException(url, ex);
@@ -162,11 +171,14 @@ abstract class RedisConnectionConfiguration {
private final boolean useSsl;
private final String username;
private final String password;
ConnectionInfo(URI uri, boolean useSsl, String password) {
ConnectionInfo(URI uri, boolean useSsl, String username, String password) {
this.uri = uri;
this.useSsl = useSsl;
this.username = username;
this.password = password;
}
@@ -182,6 +194,10 @@ abstract class RedisConnectionConfiguration {
return this.uri.getPort();
}
String getUsername() {
return this.username;
}
String getPassword() {
return this.password;
}

View File

@@ -51,6 +51,11 @@ public class RedisProperties {
*/
private String host = "localhost";
/**
* Login username of the redis server.
*/
private String username;
/**
* Login password of the redis server.
*/
@@ -118,6 +123,14 @@ public class RedisProperties {
this.host = host;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}