【鱼皮大佬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 策略
相关推荐
岁月宁静5 小时前
深度定制:在 Vue 3.5 应用中集成流式 AI 写作助手的实践
前端·vue.js·人工智能
一勺菠萝丶6 小时前
「您的连接不是私密连接」详解:为什么 HTTPS 证书会报错,以及如何正确配置子域名证书
数据库·网络协议·https
夕泠爱吃糖6 小时前
HTTPS与HTPP的区别
网络协议·http·https
2501_915909066 小时前
“绑定 HTTPS” 的工程全流程 从证书配置到真机验证与故障排查
网络协议·http·ios·小程序·https·uni-app·iphone
百锦再7 小时前
Vue Scoped样式混淆问题详解与解决方案
java·前端·javascript·数据库·vue.js·学习·.net
2501_915918417 小时前
iOS 混淆实战 多工具组合完成 IPA 混淆、加固与工程化落地(iOS混淆|IPA加固|无源码混淆|Ipa Guard|Swift Shield)
android·ios·小程序·https·uni-app·iphone·webview
Sheldon一蓑烟雨任平生7 小时前
Vue3 表单输入绑定
vue.js·vue3·v-model·vue3 表单输入绑定·表单输入绑定·input和change区别·vue3 双向数据绑定
catoop9 小时前
Sprintf Boot 之 Nacos 配置中心实践(spring.config.import=optional:nacos:)
spring·springboot
YUELEI1189 小时前
Vue 安装依赖的集合和小知识
javascript·vue.js·ecmascript
是梦终空10 小时前
计算机毕业设计241—基于Java+Springboot+vue的爱心公益服务系统(源代码+数据库+11000字文档)
java·spring boot·vue·毕业设计·课程设计·毕业论文·爱心公益系统