diff --git a/service-base/src/main/java/fi/nls/oskari/cache/JedisManager.java b/service-base/src/main/java/fi/nls/oskari/cache/JedisManager.java index 7dba0afba6..2e9567e324 100755 --- a/service-base/src/main/java/fi/nls/oskari/cache/JedisManager.java +++ b/service-base/src/main/java/fi/nls/oskari/cache/JedisManager.java @@ -36,6 +36,19 @@ public class JedisManager { */ private JedisManager() {} + public static HostAndPort getHostAndPort() { + return new HostAndPort(JedisManager.getHost(), JedisManager.getPort()); + } + + public static JedisClientConfig getClientConfig() { + return DefaultJedisClientConfig.builder() + .connectionTimeoutMillis(getConnectionTimeoutMs()) + .user(getUser()) + .password(getPassword()) + .ssl(getUseSSL()) + .build(); + } + public static String getHost() { return PropertyUtil.get(KEY_REDIS_HOSTNAME, "localhost"); } @@ -51,6 +64,9 @@ private static int getConnectionTimeoutMs() { private static String getPassword() { return PropertyUtil.get("redis.password", null); } + private static String getUser() { + return PropertyUtil.get("redis.user", null); + } private static boolean getUseSSL() { return PropertyUtil.getOptional("redis.ssl", false); } @@ -80,7 +96,7 @@ public static void connect(final int poolSize, final String host, final int port poolConfig.setTestOnBorrow(true); poolConfig.setBlockWhenExhausted(getBlockWhenExhausted()); final JedisPool oldPool = pool; - pool = new JedisPool(poolConfig, host, port, getConnectionTimeoutMs(), getPassword(), getUseSSL()); + pool = new JedisPool(poolConfig, host, port, getConnectionTimeoutMs(), getUser(), getPassword(), getUseSSL()); // Should we use the long format to have an option to pass "client name" to Redis to help debugging issues with shared Redis instances? // pool = new JedisPool(poolConfig, host, port, getConnectionTimeoutMs(), getSocketReadTimeoutMs(), getPassword(), Protocol.DEFAULT_DATABASE, getClientName()); diff --git a/service-base/src/main/java/org/oskari/cluster/ClusterClient.java b/service-base/src/main/java/org/oskari/cluster/ClusterClient.java index 4b210f7bc7..3f0e7ce1fc 100644 --- a/service-base/src/main/java/org/oskari/cluster/ClusterClient.java +++ b/service-base/src/main/java/org/oskari/cluster/ClusterClient.java @@ -4,8 +4,7 @@ import fi.nls.oskari.log.LogFactory; import fi.nls.oskari.log.Logger; import fi.nls.oskari.util.OskariRuntimeException; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPubSub; +import redis.clients.jedis.*; import java.util.*; import java.util.concurrent.ExecutorService; @@ -153,7 +152,7 @@ private void startListening(String prefix) { // NOTE!! create a new client for subscriptions instead of using pool to make sure clients don't conflict private Jedis createClient() { if (client == null) { - client = new Jedis(JedisManager.getHost(), JedisManager.getPort()); + client = new Jedis(JedisManager.getHostAndPort(), JedisManager.getClientConfig()); } return client; } diff --git a/servlet-map/src/main/java/fi/nls/oskari/spring/session/RedisSessionConfig.java b/servlet-map/src/main/java/fi/nls/oskari/spring/session/RedisSessionConfig.java index f6472296c7..996b3652d5 100644 --- a/servlet-map/src/main/java/fi/nls/oskari/spring/session/RedisSessionConfig.java +++ b/servlet-map/src/main/java/fi/nls/oskari/spring/session/RedisSessionConfig.java @@ -5,10 +5,12 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; +import org.springframework.data.redis.connection.RedisPassword; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import redis.clients.jedis.JedisClientConfig; // TODO: Check if maxInactiveIntervalInSeconds can be configured @Configuration @@ -22,7 +24,14 @@ public class RedisSessionConfig extends WebMvcConfigurerAdapter { public JedisConnectionFactory connectionFactory() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration( JedisManager.getHost(), JedisManager.getPort()); + JedisClientConfig clientConfig = JedisManager.getClientConfig(); + config.setUsername(clientConfig.getUser()); + + RedisPassword pw = RedisPassword.of(clientConfig.getPassword()); + config.setPassword(pw); JedisConnectionFactory jedis = new JedisConnectionFactory(config); + // for some reason a call to afterPropertiesSet() is required for user/passwd to be used + jedis.afterPropertiesSet(); return jedis; } }