From aca650bf88664e3a019540a5858cba5bb0dca93c Mon Sep 17 00:00:00 2001 From: caoli5288 Date: Fri, 25 Sep 2020 15:19:21 +0800 Subject: [PATCH] Redis passwords --- pom.xml | 2 +- src/main/java/com/mengcraft/simpleorm/ORM.java | 8 +++++--- .../mengcraft/simpleorm/redis/RedisProviders.java | 14 ++++++++++---- src/main/resources/config.yml | 1 + 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 614e06d..b879fce 100644 --- a/pom.xml +++ b/pom.xml @@ -48,7 +48,7 @@ org.mongodb mongo-java-driver - 2.14.2 + 2.14.3 diff --git a/src/main/java/com/mengcraft/simpleorm/ORM.java b/src/main/java/com/mengcraft/simpleorm/ORM.java index 475f9ce..d0194df 100644 --- a/src/main/java/com/mengcraft/simpleorm/ORM.java +++ b/src/main/java/com/mengcraft/simpleorm/ORM.java @@ -14,6 +14,7 @@ import lombok.SneakyThrows; import lombok.val; import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.entity.Player; import org.bukkit.metadata.FixedMetadataValue; @@ -79,9 +80,10 @@ public void onEnable() { new MetricsLite(this); if (nil(globalRedisWrapper)) { if (redisProvider == null) { - String redisUrl = getConfig().getString("redis.url", ""); - int max = getConfig().getInt("redis.max_conn", -1); - redisProvider = RedisProviders.of(getConfig().getString("redis.master_name"), redisUrl, max); + FileConfiguration config = getConfig(); + String redisUrl = config.getString("redis.url", ""); + int max = config.getInt("redis.max_conn", -1); + redisProvider = RedisProviders.of(config.getString("redis.master_name"), redisUrl, max, config.getString("redis.password")); } globalRedisWrapper = new RedisWrapper(redisProvider); } diff --git a/src/main/java/com/mengcraft/simpleorm/redis/RedisProviders.java b/src/main/java/com/mengcraft/simpleorm/redis/RedisProviders.java index cc6cae7..9d492ab 100644 --- a/src/main/java/com/mengcraft/simpleorm/redis/RedisProviders.java +++ b/src/main/java/com/mengcraft/simpleorm/redis/RedisProviders.java @@ -1,5 +1,6 @@ package com.mengcraft.simpleorm.redis; +import com.mengcraft.simpleorm.lib.Utils; import com.mengcraft.simpleorm.provider.IRedisProvider; import lombok.RequiredArgsConstructor; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; @@ -14,13 +15,16 @@ public class RedisProviders { - public static IRedisProvider of(String sentinel, String url, int conn) { + public static IRedisProvider of(String sentinel, String url, int conn, String password) { GenericObjectPoolConfig config = new GenericObjectPoolConfig<>(); if (conn >= 1) { config.setMaxTotal(conn); } - if (sentinel == null || sentinel.isEmpty()) { - return new GenericProvider(new JedisPool(config, URI.create(url))); + if (Utils.isNullOrEmpty(sentinel)) { + URI remote = URI.create(url); + return new GenericProvider(Utils.isNullOrEmpty(password) ? + new JedisPool(config, remote) : + new JedisPool(config, remote.getHost(), remote.getPort(), 2000, password)); } Set sentinels = new HashSet<>(); if (url.matches("redis://(.+[,].+)")) { @@ -32,7 +36,9 @@ public static IRedisProvider of(String sentinel, String url, int conn) { sentinels.add(uri.getHost() + ':' + uri.getPort()); } } - return new SentinelProvider(new JedisSentinelPool(sentinel, sentinels, config)); + return new SentinelProvider(Utils.isNullOrEmpty(password) ? + new JedisSentinelPool(sentinel, sentinels, config) : + new JedisSentinelPool(sentinel, sentinels, config, password)); } @RequiredArgsConstructor diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 8a8bf81..23823ca 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -7,6 +7,7 @@ redis: master_name: null url: redis://localhost:6379 max_conn: 20 + password: null mongo: url: mongodb://localhost \ No newline at end of file