Skip to content

Commit

Permalink
feat: add new constructor to support Redis username (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
alordtas authored Mar 27, 2024
1 parent abbf498 commit 0f92b7b
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/main/java/org/casbin/adapter/RedisAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 0f92b7b

Please sign in to comment.