From dda14fb93619c8a819fbac0f54e08540b5a5ba2e Mon Sep 17 00:00:00 2001
From: click33 <2393584716@qq.com>
Date: Fri, 23 Jun 2023 03:15:51 +0800
Subject: [PATCH] =?UTF-8?q?alone-redis=20=E6=8F=92=E4=BB=B6=E5=85=BC?=
=?UTF-8?q?=E5=AE=B9=E4=BD=8E=E7=89=88=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../sa-token-demo-alone-redis/pom.xml | 1 +
.../satoken/dao/alone/SaAloneRedisInject.java | 35 ++++++++++++++++---
2 files changed, 31 insertions(+), 5 deletions(-)
diff --git a/sa-token-demo/sa-token-demo-alone-redis/pom.xml b/sa-token-demo/sa-token-demo-alone-redis/pom.xml
index 51927ffb..8688e25e 100644
--- a/sa-token-demo/sa-token-demo-alone-redis/pom.xml
+++ b/sa-token-demo/sa-token-demo-alone-redis/pom.xml
@@ -11,6 +11,7 @@
org.springframework.boot
spring-boot-starter-parent
2.5.14
+
diff --git a/sa-token-plugin/sa-token-alone-redis/src/main/java/cn/dev33/satoken/dao/alone/SaAloneRedisInject.java b/sa-token-plugin/sa-token-alone-redis/src/main/java/cn/dev33/satoken/dao/alone/SaAloneRedisInject.java
index 4bd10a53..f97acc1f 100644
--- a/sa-token-plugin/sa-token-alone-redis/src/main/java/cn/dev33/satoken/dao/alone/SaAloneRedisInject.java
+++ b/sa-token-plugin/sa-token-alone-redis/src/main/java/cn/dev33/satoken/dao/alone/SaAloneRedisInject.java
@@ -95,13 +95,23 @@ public class SaAloneRedisInject implements EnvironmentAware{
redisConfig.setDatabase(cfg.getDatabase());
redisConfig.setPassword(RedisPassword.of(cfg.getPassword()));
redisConfig.setDatabase(cfg.getDatabase());
- redisConfig.setUsername(cfg.getUsername());
+ // 低版本没有 username 属性,捕获异常给个提示即可,无需退出程序
+ try {
+ redisConfig.setUsername(cfg.getUsername());
+ } catch (NoSuchMethodError e){
+ System.err.println(e.getMessage());
+ }
redisAloneConfig = redisConfig;
} else if (pattern.equals("cluster")){
// 普通集群模式
RedisClusterConfiguration redisClusterConfig = new RedisClusterConfiguration();
- redisClusterConfig.setUsername(cfg.getUsername());
+ // 低版本没有 username 属性,捕获异常给个提示即可,无需退出程序
+ try {
+ redisClusterConfig.setUsername(cfg.getUsername());
+ } catch (NoSuchMethodError e){
+ System.err.println(e.getMessage());
+ }
redisClusterConfig.setPassword(RedisPassword.of(cfg.getPassword()));
RedisProperties.Cluster cluster = cfg.getCluster();
@@ -117,7 +127,12 @@ public class SaAloneRedisInject implements EnvironmentAware{
// 哨兵集群模式
RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
redisSentinelConfiguration.setDatabase(cfg.getDatabase());
- redisSentinelConfiguration.setUsername(cfg.getUsername());
+ // 低版本没有 username 属性,捕获异常给个提示即可,无需退出程序
+ try {
+ redisSentinelConfiguration.setUsername(cfg.getUsername());
+ } catch (NoSuchMethodError e){
+ System.err.println(e.getMessage());
+ }
redisSentinelConfiguration.setPassword(RedisPassword.of(cfg.getPassword()));
RedisProperties.Sentinel sentinel = cfg.getSentinel();
@@ -134,7 +149,12 @@ public class SaAloneRedisInject implements EnvironmentAware{
// socket 连接单体 Redis
RedisSocketConfiguration redisSocketConfiguration = new RedisSocketConfiguration();
redisSocketConfiguration.setDatabase(cfg.getDatabase());
- redisSocketConfiguration.setUsername(cfg.getUsername());
+ // 低版本没有 username 属性,捕获异常给个提示即可,无需退出程序
+ try {
+ redisSocketConfiguration.setUsername(cfg.getUsername());
+ } catch (NoSuchMethodError e){
+ System.err.println(e.getMessage());
+ }
redisSocketConfiguration.setPassword(RedisPassword.of(cfg.getPassword()));
String socket = environment.getProperty(ALONE_PREFIX + ".socket", "");
redisSocketConfiguration.setSocket(socket);
@@ -147,7 +167,12 @@ public class SaAloneRedisInject implements EnvironmentAware{
int port = cfg.getPort();
RedisStaticMasterReplicaConfiguration redisStaticMasterReplicaConfiguration = new RedisStaticMasterReplicaConfiguration(hostName, port);
redisStaticMasterReplicaConfiguration.setDatabase(cfg.getDatabase());
- redisStaticMasterReplicaConfiguration.setUsername(cfg.getUsername());
+ // 低版本没有 username 属性,捕获异常给个提示即可,无需退出程序
+ try {
+ redisStaticMasterReplicaConfiguration.setUsername(cfg.getUsername());
+ } catch (NoSuchMethodError e){
+ System.err.println(e.getMessage());
+ }
redisStaticMasterReplicaConfiguration.setPassword(RedisPassword.of(cfg.getPassword()));
redisAloneConfig = redisStaticMasterReplicaConfiguration;