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 pair
at 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]));