Feature - username parameter added. #2634

pull/2637/head
Nikita Koksharov 5 years ago
parent 80afc1931e
commit 948cbb121d

@ -45,7 +45,8 @@ public class RedisClientConfig {
private Class<? extends SocketChannel> socketChannelClass = NioSocketChannel.class;
private int connectTimeout = 10000;
private int commandTimeout = 10000;
private String username;
private String password;
private int database;
private String clientName;
@ -80,6 +81,7 @@ public class RedisClientConfig {
this.connectTimeout = config.connectTimeout;
this.commandTimeout = config.commandTimeout;
this.password = config.password;
this.username = config.username;
this.database = config.database;
this.clientName = config.clientName;
this.readOnly = config.readOnly;
@ -312,7 +314,13 @@ public class RedisClientConfig {
this.resolverGroup = resolverGroup;
return this;
}
public String getUsername() {
return username;
}
public RedisClientConfig setUsername(String username) {
this.username = username;
return this;
}
}

@ -65,7 +65,12 @@ public abstract class BaseConnectionHandler<C extends RedisConnection> extends C
RedisClientConfig config = redisClient.getConfig();
if (config.getPassword() != null) {
RFuture<Object> future = connection.async(RedisCommands.AUTH, config.getPassword());
RFuture<Object> future;
if (config.getUsername() != null) {
future = connection.async(RedisCommands.AUTH, config.getUsername(), config.getPassword());
} else {
future = connection.async(RedisCommands.AUTH, config.getPassword());
}
futures.add(future);
}
if (config.getDatabase() != 0) {

@ -69,6 +69,8 @@ public class BaseConfig<T extends BaseConfig<T>> {
*/
private String password;
private String username;
/**
* Subscriptions per Redis connection limit
*/
@ -103,6 +105,7 @@ public class BaseConfig<T extends BaseConfig<T>> {
BaseConfig(T config) {
setPassword(config.getPassword());
setUsername(config.getUsername());
setSubscriptionsPerConnection(config.getSubscriptionsPerConnection());
setRetryAttempts(config.getRetryAttempts());
setRetryInterval(config.getRetryInterval());
@ -124,7 +127,8 @@ public class BaseConfig<T extends BaseConfig<T>> {
/**
* Subscriptions per Redis connection limit
* Default is 5
* <p>
* Default is <code>5</code>
*
* @param subscriptionsPerConnection amount
* @return config
@ -139,7 +143,8 @@ public class BaseConfig<T extends BaseConfig<T>> {
}
/**
* Password for Redis authentication. Should be null if not needed
* Password for Redis authentication. Should be null if not needed.
* <p>
* Default is <code>null</code>
*
* @param password for connection
@ -154,6 +159,25 @@ public class BaseConfig<T extends BaseConfig<T>> {
return password;
}
/**
* Username for Redis authentication. Should be null if not needed
* <p>
* Default is <code>null</code>
* <p>
* Requires Redis 6.0+
*
* @param username for connection
* @return config
*/
public T setUsername(String username) {
this.username = username;
return (T) this;
}
public String getUsername() {
return username;
}
/**
* Error will be thrown if Redis command can't be sent to Redis server after <code>retryAttempts</code>.
* But if it sent successfully then <code>timeout</code> will be started.
@ -176,7 +200,6 @@ public class BaseConfig<T extends BaseConfig<T>> {
/**
* Defines time interval for another one attempt send Redis command
* if it hasn't been sent already.
*
* <p>
* Default is <code>1500</code> milliseconds
*
@ -212,6 +235,8 @@ public class BaseConfig<T extends BaseConfig<T>> {
/**
* Setup connection name during connection init
* via CLIENT SETNAME command
* <p>
* Default is <code>null</code>
*
* @param clientName - name of client
* @return config
@ -313,7 +338,7 @@ public class BaseConfig<T extends BaseConfig<T>> {
/**
* Defines SSL provider used to handle SSL connections.
* <p>
* Default is JDK
* Default is <code>JDK</code>
*
* @param sslProvider - ssl provider
* @return config
@ -329,7 +354,9 @@ public class BaseConfig<T extends BaseConfig<T>> {
/**
* Defines path to SSL truststore
*
* <p>
* Default is <code>null</code>
*
* @param sslTruststore - path
* @return config
*/
@ -344,7 +371,9 @@ public class BaseConfig<T extends BaseConfig<T>> {
/**
* Defines password for SSL truststore
*
* <p>
* Default is <code>null</code>
*
* @param sslTruststorePassword - password
* @return config
*/
@ -359,7 +388,9 @@ public class BaseConfig<T extends BaseConfig<T>> {
/**
* Defines path to SSL keystore
*
* <p>
* Default is <code>null</code>
*
* @param sslKeystore - path to keystore
* @return config
*/
@ -374,7 +405,9 @@ public class BaseConfig<T extends BaseConfig<T>> {
/**
* Defines password for SSL keystore
*
* <p>
* Default is <code>null</code>
*
* @param sslKeystorePassword - password
* @return config
*/

@ -414,6 +414,7 @@ public class MasterSlaveConnectionManager implements ConnectionManager {
c.setPingTimeout(cfg.getPingTimeout());
c.setLoadBalancer(cfg.getLoadBalancer());
c.setPassword(cfg.getPassword());
c.setUsername(cfg.getUsername());
c.setClientName(cfg.getClientName());
c.setMasterConnectionPoolSize(cfg.getMasterConnectionPoolSize());
c.setSlaveConnectionPoolSize(cfg.getSlaveConnectionPoolSize());
@ -483,6 +484,7 @@ public class MasterSlaveConnectionManager implements ConnectionManager {
.setPingConnectionInterval(config.getPingConnectionInterval())
.setKeepAlive(config.isKeepAlive())
.setTcpNoDelay(config.isTcpNoDelay())
.setUsername(config.getUsername())
.setPassword(config.getPassword())
.setNettyHook(cfg.getNettyHook());

@ -50,6 +50,7 @@ public class SingleConnectionManager extends MasterSlaveConnectionManager {
newconfig.setTimeout(cfg.getTimeout());
newconfig.setPingTimeout(cfg.getPingTimeout());
newconfig.setPassword(cfg.getPassword());
newconfig.setUsername(cfg.getUsername());
newconfig.setDatabase(cfg.getDatabase());
newconfig.setClientName(cfg.getClientName());
newconfig.setMasterAddress(cfg.getAddress());

@ -15,15 +15,8 @@
*/
package org.redisson.connection.pool;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import org.redisson.api.NodeType;
import org.redisson.api.RFuture;
import org.redisson.client.RedisConnection;
@ -40,8 +33,14 @@ import org.redisson.misc.RedissonPromise;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
/**
* Base connection pool class
@ -416,24 +415,13 @@ abstract class ConnectionPool<T extends RedisConnection> {
}
};
if (entry.getConfig().getPassword() != null) {
RFuture<Void> temp = c.async(RedisCommands.AUTH, config.getPassword());
temp.onComplete((res, ex) -> {
ping(c, pingListener);
});
} else {
ping(c, pingListener);
}
RFuture<String> f = c.async(RedisCommands.PING);
f.onComplete(pingListener);
});
}
}, config.getFailedSlaveReconnectionInterval(), TimeUnit.MILLISECONDS);
}
private void ping(RedisConnection c, BiConsumer<String, Throwable> pingListener) {
RFuture<String> f = c.async(RedisCommands.PING);
f.onComplete(pingListener);
}
public void returnConnection(ClientConnectionsEntry entry, T connection) {
if (entry.isFreezed() && entry.getFreezeReason() != FreezeReason.SYSTEM) {
connection.closeAsync();

Loading…
Cancel
Save