【鱼皮大佬API开放平台项目】Spring Cloud Gateway HTTPS 配置问题解决方案总结

问题背景

项目架构为前后端分离的微服务架构:

  • 前端部署在 8000 端口
  • API 网关部署在 9000 端口
  • 后端服务包括:
    • api-backend (9001端口)
    • api-interface (9002端口)

初始状态:

  • 前端已配置 HTTPS(端口 8000)
  • 后端服务未配置 HTTPS
  • 通过 Nginx 进行反向代理

遇到的问题

  1. 第一阶段:400 Bad Request

    This combination of host and port requires TLS

原因:前端使用 HTTPS,但后端服务未正确配置 SSL 证书。由于前端首先请求是到达api-gateway 网关层。借助Spring Cloud Gateway 进行请求转发。虽然给API gateway分配了https,但是它转发的另外两个模块的端口没有分配HTTPS导致请求转发错误。

  1. 第二阶段:403 错误

    Failed to resolve 'api-backend' after 4 queries

原因:网关路由配置使用服务名而非具体地址,导致服务发现失败。

项目配置文件

解决方案

1. SSL 证书配置

为所有服务配置 SSL:

  1. API Gateway (9000端口):
  • application.yml 配置
yaml 复制代码
server:
  port: 9000
  address: 0.0.0.0
  ssl:
    enabled: true
    key-store: /www/server/nginx/ssl/<服务器ip>/keystore.p12
    key-store-password: 1234
    key-store-type: PKCS12
  http2:
    enabled: true
spring:
  cloud:
    gateway:
      routes:
        - id: api-backend
          uri: https://<服务器ip>:9001
          predicates: 
            - Path=/api/backend/**
          filters:
            - StripPrefix=0
            - AddRequestHeader=X-Forwarded-Proto,https
        - id: api-interface
          uri: https://<服务器ip>:9002
          predicates: 
            - Path=/api/interface/**
          filters:
            - StripPrefix=0
            - AddRequestHeader=X-Forwarded-Proto,https
      ssl:
        key-store: /www/server/nginx/ssl/<服务器ip>/keystore.p12
        key-store-password: 1234
        key-store-type: PKCS12

logging:
  level:
    org:
      springframework:
        cloud:
          gateway: DEBUG

dubbo:
  application:
    name: api-gateway-dubbo
    qosEnable: false
    enable-file-cache: false
  protocol:
    name: dubbo
    port: -1
    # 注意这里ssl也需要配置
  ssl:
    enabled: true
  • application-prod.yml
yml 复制代码
spring:
  cloud:
    nacos:
      server-addr: 192.168.15.233:8848
      secure: true   # 添加这行,启用安全连接
      config:
        file-extension: yaml
  • bootstrap.yml
yml 复制代码
spring:
  cloud:
    nacos:
      server-addr: 192.168.15.233:8848
      secure: true   # 添加这行,启用安全连接
      config:
        file-extension: yaml
  application:
    name: api-interface
  1. API-backend (9001端口)
  • application.yml配置
yml 复制代码
server:
  port: 9001
  address: 0.0.0.0
  ssl:
    enabled: true
    key-store: /www/server/nginx/ssl/<服务器ip>/keystore.p12
    key-store-password: 1234
    key-store-type: PKCS12
  http2:
    enabled: true
  servlet:
    context-path: /api/backend
    session:
      cookie:
        max-age: 7d
        secure: true

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 100MB
  session:
    store-type: redis
    timeout: 7d

knife4j:
  enable: true
  basic:
    enable: true
    username: root
    password: xxx
dubbo:
  application:
    name: api-backend-dubbo
    qosEnable: false
    enable-file-cache: false
  protocol:
    name: dubbo
    port: -1
  registry:
    address: nacos://192.168.15.233:8848
  ssl:
    enabled: true # 注意这里true
  • application-prod.yml
yml 复制代码
# 客户端SDK 配置
api:
  client:
    gateway-host: https://<服务器ip>:9000
  • bootstrap.yml
yml 复制代码
spring:
  application:
    name: api-backend
  profiles:
    active: prod
  cloud:
    nacos:
      server-addr: 192.168.15.233:8848
      secure: true   # 添加这行,启用安全连接
      config:
        file-extension: yaml
  1. API- interface
  • application.yml
yml 复制代码
server:
  port: 9002
  address: 0.0.0.0
  ssl:
    enabled: true
    key-store: /www/server/nginx/ssl/<服务器ip>/keystore.p12
    key-store-password: 1234
    key-store-type: PKCS12
  http2:
    enabled: true
  servlet:
    context-path: /api/interface
#-------------------------------------------------
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
dubbo:
  ssl:
    enabled: true     
  • application-prod.yml
yml 复制代码
spring:
  cloud:
    nacos:
      server-addr: 192.168.15.233:8848
      secure: true   # 添加这行,启用安全连接
      config:
        file-extension: yaml
  • bootstrap.yml
yml 复制代码
spring:
  cloud:
    nacos:
      server-addr: 192.168.15.233:8848
      secure: true   # 添加这行,启用安全连接
      config:
        file-extension: yaml
  application:
    name: api-interface

2. Nginx 配置

更新 Nginx 配置以支持 HTTPS 代理:

nginx 复制代码
location /api {
    rewrite ^/api/(.*) /$1 break;
    proxy_pass https://<服务器ip>:9000/api;
    proxy_ssl_verify off;
    proxy_ssl_server_name on;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
}

关键配置点说明

  1. SSL 证书配置

    • 所有服务统一使用相同的 SSL 证书
    • 启用 HTTP2 以提升性能
    • 确保证书文件权限正确
  2. 网关路由

    • 使用具体的服务地址而不是服务名
    • 添加必要的请求头
    • 保持 SSL 配置一致性
  3. 服务间通信

    • Dubbo 服务启用 SSL
    • 确保所有通信链路使用 HTTPS

最佳实践建议

  1. 证书管理

    • 使用统一的证书存储位置
    • 确保证书文件权限正确
    • 定期更新证书
  2. 配置管理

    • 使用配置中心统一管理
    • 环境隔离(开发/生产)
    • 密码等敏感信息加密存储
  3. 安全增强

    • 启用 HTTP2
    • 配置安全的 SSL 协议和密码套件
    • 启用 HSTS 策略
相关推荐
zhyoobo13 分钟前
IPv4 与 IPv6:网络世界的世代更替与未来之路
网络·http·ipv4·ipv6
XMYX-014 分钟前
Java HTTPS 请求失败排查与证书导入全过程
java·https
随便起的名字也被占用34 分钟前
leaflet中绘制轨迹线的大量轨迹点,解决大量 marker 绑定 tooltip 同时显示导致的性能问题
前端·javascript·vue.js·leaflet
2501_9159184140 分钟前
iOS 抓不到包怎么办?全流程排查思路与替代引导
android·ios·小程序·https·uni-app·iphone·webview
天下无贼!1 小时前
【自制组件库】从零到一实现属于自己的 Vue3 组件库!!!
前端·javascript·vue.js·ui·架构·scss
知识分享小能手2 小时前
Vue3 学习教程,从入门到精通,Vue3 中使用 Axios 进行 Ajax 请求的语法知识点与案例代码(23)
前端·javascript·vue.js·学习·ajax·vue·vue3
JavaArchJourney2 小时前
Spring Cloud Eureka 核心原理
spring cloud
码农101号3 小时前
Linux中Docker Swarm介绍和使用
linux·spring cloud·docker
白日依山尽yy3 小时前
springcloud04——网关gateway、熔断器 sentinel
spring cloud
前端小巷子3 小时前
Vue 2 Diff 算法
前端·vue.js·面试