Spring集成Redis中禁用主机名DNS检测

问题

最近使用ssh隧道代理redis服务器。在Spring中正常配置了ssl方式连接redis后,出现了如下错误:

Caused by: java.security.cert.CertificateException: No subject alternative DNS name matching localhost found.

Spring连接redis配置如下:

yaml 复制代码
spring: 
  # redis 配置
  redis:
    # 地址
    host: localhost
    # 端口,默认为6379
    port: 6379
    # 数据库索引
    database: 0
    # 密码
    password: xxxx
    # 连接超时时间
    timeout: 10s
    lettuce:
      pool:
        # 连接池中的最小空闲连接
        min-idle: 0
        # 连接池中的最大空闲连接
        max-idle: 8
        # 连接池的最大数据库连接数
        max-active: 8
        # #连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1ms
    ssl: true

解决

配置一个redis类,将禁用DNS检测,内容如下:

java 复制代码
package com.xxx.web.core.config;

import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedisSslConfig {
    @Bean
    public LettuceClientConfigurationBuilderCustomizer lettuceClientCustomizer() {
        return builder -> builder
                .useSsl()
                .disablePeerVerification(); // <== 禁用主机名校验 生产和UAT环境不能使用该配置
    }
}

注意,这是老版本的Spring就只能这样配置了。生产环境是不会存在这样的问题的,生产环境可以直接访问redis集群的,这里只是为了解决开发环境SSH隧道代理Redis集群问题。

总结

我这里是老Spring版本配置而已。

参考

相关推荐
雨中飘荡的记忆17 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
用户83071968408221 小时前
spring ai alibaba + nacos +mcp 实现mcp服务负载均衡调用实战
spring boot·spring·mcp
曲幽1 天前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
NE_STOP4 天前
springMVC-HTTP消息转换器与文件上传、下载、异常处理
spring
JavaGuide5 天前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
玹外之音5 天前
Spring AI MCP 实战:将你的服务升级为 AI 可调用的智能工具
spring·ai编程
来一斤小鲜肉5 天前
Spring AI入门:第一个AI应用跑起来
spring·ai编程
埃博拉酱5 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
NE_STOP5 天前
springMVC-常见视图组件与RESTFul编程风格
spring