【问题标题】:How to pass username and password to Jedis?如何将用户名和密码传递给 Jedis?
【发布时间】:2022-11-19 10:46:55
【问题描述】:

我正在尝试将 Redis 6.0 ACL 与 Sentinel 一起使用以实现高可用性。目前,只有 Redis 服务器借助 ACL 功能配置了用户名和密码,没有为 Sentinels 配置凭证。

我正在使用 Jedis 与 Redis 交互,但 Jedis 库似乎没有提供指定用户名和密码的方法。请在下面找到配置

#redis-server.conf
bind 172.x.x.x 127.0.0.1
protected-mode no
daemonize yes
logfile "redis-server.log"
pidfile "/var/run/redis.pid"
dir "server"
masterauth "***"
masteruser "replica-user"

aclfile "users.acl"


#redis-sentinel.conf
bind 172.x.x.x 127.0.0.1
protected-mode no
port 26379
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "redis-sentinel.log"
dir "sentinel"
sentinel myid 682b15dd6d8606dde7e8802a5aebb1c15ab1864e
sentinel deny-scripts-reconfig yes
sentinel monitor rediscluster 172.x.x.x 6379 2
sentinel down-after-milliseconds rediscluster 5000
sentinel failover-timeout rediscluster 60000
sentinel auth-pass rediscluster 123456
# Generated by CONFIG REWRITE
user default on nopass ~* +@all
sentinel auth-user rediscluster sentinel-user
sentinel config-epoch rediscluster 1
sentinel leader-epoch rediscluster 1
sentinel known-replica rediscluster 172.x.x.x 6379
sentinel known-replica rediscluster 172.x.x.x 6379
sentinel known-sentinel rediscluster 172.x.x.x 26379 f2083b5ea2e419fb38c538314f5ed35439ea8572
sentinel known-sentinel rediscluster 172.x.x.x 26379 cc85a1fd3265797c883891609ac5b4e182b9b99a
sentinel current-epoch 1

#users.acl
user default off -@all
user sentinel-user on >123456 +multi +slaveof +ping +exec +subscribe +config|rewrite +role +publish +info +client|setname +client|kill +script|kill
user replica-user on >123456 +psync +replconf +ping
user app-user on >123456 ~* +@all

下面是创建 JedisConnectionFactory 的 java 代码,但我没有找到任何合适的方法来传递用户名和密码

@Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        redisSentinelConfiguration.setMaster(env.getProperty(Constant.REDIS_MASTER_NAME_KEY));
        redisSentinelConfiguration.setSentinels(getRedisNodes(env.getProperty(Constant.REDIS_HOST_KEY)));

        Integer conTimeOutInSecs = Integer.valueOf(env.getProperty(Constant.REDIS_CONNECT_TIMEOUT_KEY));
        Integer maxTotalConn = Integer.valueOf(env.getProperty(Constant.REDIS_MAX_TOTAL_CONN_KEY));
        Integer minIdleConn = Integer.valueOf(env.getProperty(Constant.REDIS_MIN_IDLE_CONN_KEY));
        Integer maxIdleConn = Integer.valueOf(env.getProperty(Constant.REDIS_MAX_IDLE_CONN_KEY));

        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(maxTotalConn);
        poolConfig.setMinIdle(minIdleConn);
        poolConfig.setMaxIdle(maxIdleConn);
        JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder()
                .clientName(env.getProperty(Constant.APPLICATION_NAME_KEY))
                .usePooling().poolConfig(poolConfig).and().connectTimeout(Duration.ofSeconds(conTimeOutInSecs)).build();
        JedisConnectionFactory jedisConFactory = new JedisConnectionFactory(redisSentinelConfiguration,
                jedisClientConfiguration);

        return jedisConFactory;
    }

在创建 JedisConnectionFactory 时,有什么方法可以传递用户名和密码吗?

【问题讨论】:

  • 通过将 spring-data-redis 升级到 2.6.3 解决了这个问题。
  • 你能发布一个详细的答案吗?我不太了解您的评论如何解决 Jedis 用户/通行证问题。

标签: redis spring-data-redis jedis


【解决方案1】:

// You can try this code with jedis:4.0.0. // redis.clients.jedis.DefaultJedisClientConfig

DefaultJedisClientConfig defaultJedisClientConfig = DefaultJedisClientConfig.builder().user("vboxredis").password("vboxredis001").build(); Jedis jedis = new Jedis("192.168.56.101",63791,defaultJedisClientConfig);

【讨论】:

    猜你喜欢
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 2016-07-01
    相关资源
    最近更新 更多