Java Lettuce Redis 客户端中的 RedisCommandExecutionException

RedisCommandExecutionException in Java Lettuce Redis client

我在使用 ACL 的 Java Lettuce Redis 客户端时遇到问题:我收到 io.lettuce.core.RedisCommandExecutionException: WRONGPASS invalid username-password pair or user is disabled 异常。 我有一个 Redis Docker 容器配置如下:

我的Dockerfile:

FROM redis:6.2.1

COPY redis.conf /usr/local/etc/redis/redis.conf
COPY users.acl /etc/redis/users.acl

EXPOSE 6379

CMD ["redis-server", "/usr/local/etc/redis/redis.conf"]

redis.conf 文件是:

aclfile /etc/redis/users.acl

并且 users.acl 文件是:

user myuser on +@all ~* >mypassword
user default off

I 运行 带有命令的容器:

docker run -it -p 6379:6379 --name myredis myredis

我的 Java 客户是:

...

import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;

public class Main {
    public static void main(String... args) {
        try {
            RedisClient redisClient = RedisClient.create("redis://myuser:mypassword@localhost");
            StatefulRedisConnection<String, String> connection = redisClient.connect();
            System.out.println("Connected to Redis");
            connection.close();
            redisClient.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("End!");
        }
    }
}

当我尝试 运行 它时,出现错误:

io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78)
    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
    at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:234)
    at io.lettuce.core.RedisClient.connect(RedisClient.java:207)
    at io.lettuce.core.RedisClient.connect(RedisClient.java:192)
    at [MY_PACKAGE].Main.main(Main.java:16)
Caused by: io.lettuce.core.RedisCommandExecutionException: WRONGPASS invalid username-password pair or user is disabled.
    at io.lettuce.core.ExceptionFactory.createExecutionException(ExceptionFactory.java:135)
    at io.lettuce.core.ExceptionFactory.createExecutionException(ExceptionFactory.java:108)
    at io.lettuce.core.protocol.AsyncCommand.completeResult(AsyncCommand.java:120)
    at io.lettuce.core.protocol.AsyncCommand.complete(AsyncCommand.java:111)
    at io.lettuce.core.protocol.CommandHandler.complete(CommandHandler.java:654)
    at io.lettuce.core.protocol.CommandHandler.decode(CommandHandler.java:614)
    at io.lettuce.core.protocol.CommandHandler.channelRead(CommandHandler.java:565)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1422)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:931)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:700)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:635)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:552)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:514)
    at io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:1050)
    at io.netty.util.internal.ThreadExecutorMap.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:834)

我做错了什么?提前致谢。

这对我有用 (lettuce 6.0.4),但它是纯粹的魔法:

改变

user default off

进入

user default off -@all +hello

我阅读了 redis 和 lettuce 文档,但找不到合适的解释,但这对你来说可能更合理:

user default on nopass -@all +hello

这将使客户端能够使用默认用户来执行 运行 仅 hello 命令,然后通过某种方式,客户端将知道使用配置的用户 (myuser) 来执行进一步的命令。

希望对大家有所帮助,等待大家的解释:penguin: