From 0f92b7b3867740a5ea0b05b8032ca21a8e02cf19 Mon Sep 17 00:00:00 2001 From: Angus Lord <134388730+alordtas@users.noreply.github.com> Date: Wed, 27 Mar 2024 18:04:50 +0100 Subject: [PATCH] feat: add new constructor to support Redis username (#18) --- .../java/org/casbin/adapter/RedisAdapter.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/casbin/adapter/RedisAdapter.java b/src/main/java/org/casbin/adapter/RedisAdapter.java index 5c2b0b9..f98b6d2 100644 --- a/src/main/java/org/casbin/adapter/RedisAdapter.java +++ b/src/main/java/org/casbin/adapter/RedisAdapter.java @@ -37,14 +37,18 @@ public class RedisAdapter implements Adapter, BatchAdapter{ private String key; private Jedis jedis; - public RedisAdapter(String host, int port) { newRedisAdapter(host, port, "casbin_rules", null); } + public RedisAdapter(String host, int port) { newRedisAdapter(host, port, "casbin_rules", null, null); } public RedisAdapter(String host, int port, String password) { - newRedisAdapter(host, port, "casbin_rules", password); + newRedisAdapter(host, port, "casbin_rules", null, password); } public RedisAdapter(String host, int port, String key, String password) { - newRedisAdapter(host, port, key, password); + newRedisAdapter(host, port, key, null, password); + } + + public RedisAdapter(String host, int port, String key, String username, String password) { + newRedisAdapter(host, port, key, username, password); } /** @@ -159,12 +163,17 @@ public void selectDb(int dbIndex) { } } - private void newRedisAdapter(String host, int port, String key, String password) { + private void newRedisAdapter(String host, int port, String key, String username, String password) { this.key = key; jedis = new Jedis(host, port); if (password != null) { - jedis.auth(password); + if (username != null) { + jedis.auth(username, password); + } + else { + jedis.auth(password); + } } Util.logPrintf("Redis service is running ", jedis.ping());