当前位置: 首页> 娱乐> 明星 > Spring Data Redis 报错 WRONGPASS invalid username-password pair问题解决

Spring Data Redis 报错 WRONGPASS invalid username-password pair问题解决

时间:2025/7/18 17:05:34来源:https://blog.csdn.net/u013014691/article/details/140697238 浏览次数:0次

Spring Data Redis 报错 WRONGPASS invalid username-password pair问题解决

缘起

spring data redis版本:3.2.5
redis server版本:社区版6.0.2

新项目引入了redis,我就把原来的redis代码拷贝过来,但使用时报错:

Caused by: io.lettuce.core.RedisCommandExecutionException: WRONGPASS invalid username-password pairat io.lettuce.core.internal.ExceptionFactory.createExecutionException(ExceptionFactory.java:147)at io.lettuce.core.internal.ExceptionFactory.createExecutionException(ExceptionFactory.java:116)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:745)at io.lettuce.core.protocol.CommandHandler.decode(CommandHandler.java:680)at io.lettuce.core.protocol.CommandHandler.channelRead(CommandHandler.java:597)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788)at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)at java.base/java.lang.Thread.run(Thread.java:833)

查找问题

redis配置代码逻辑如下:

@Bean("lettuceConnectionFactory")public LettuceConnectionFactory lettuceConnectionFactory() {GenericObjectPoolConfig<RedisConnection> genericObjectPoolConfig = new GenericObjectPoolConfig<>();genericObjectPoolConfig.setMaxIdle(maxIdle);genericObjectPoolConfig.setMinIdle(minIdle);genericObjectPoolConfig.setMaxTotal(maxTotal);RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();redisStandaloneConfiguration.setDatabase(dbNo);redisStandaloneConfiguration.setHostName(host);redisStandaloneConfiguration.setPort(port);redisStandaloneConfiguration.setPassword(RedisPassword.of(password));LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(timeOut)).poolConfig(genericObjectPoolConfig).build();return new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);}@Bean("stringRedisTemplate")public StringRedisTemplate stringRedisTemplate(LettuceConnectionFactory redisConnectionFactory){StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();stringRedisTemplate.setConnectionFactory(redisConnectionFactory);Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);// 设置值(value)的序列化采用Jackson2JsonRedisSerializer。stringRedisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// 设置键(key)的序列化采用StringRedisSerializer。stringRedisTemplate.setKeySerializer(new StringRedisSerializer());stringRedisTemplate.setHashKeySerializer(new StringRedisSerializer());stringRedisTemplate.afterPropertiesSet();return stringRedisTemplate;}

怎么检查redis的host,端口和密码(格式为username:password形式)都是正确的。
最后发现,redisStandaloneConfiguration.setPassword(RedisPassword.of(password));这个要改造下。

解决

redis6.x,username和password要分开写
如果用户名和密码形式是username:password 那么5.x的写法是

redisStandaloneConfiguration.setPassword(RedisPassword.of(password));

如果用户名和密码形式是username:password 那么6.x的写法是

redisStandaloneConfiguration.setUsername(password.split(":")[0]);
redisStandaloneConfiguration.setPassword(RedisPassword.of(password.split(":")[1]));
关键字:Spring Data Redis 报错 WRONGPASS invalid username-password pair问题解决

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: