惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
月光博客
月光博客
AI
AI
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CXSECURITY Database RSS Feed - CXSecurity.com
WordPress大学
WordPress大学
GbyAI
GbyAI
L
Lohrmann on Cybersecurity
O
OpenAI News
Schneier on Security
Schneier on Security
P
Palo Alto Networks Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
W
WeLiveSecurity
L
LINUX DO - 最新话题
人人都是产品经理
人人都是产品经理
S
Security Affairs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
Arctic Wolf
Recorded Future
Recorded Future
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
GRAHAM CLULEY
N
Netflix TechBlog - Medium
TaoSecurity Blog
TaoSecurity Blog
C
Check Point Blog
Scott Helme
Scott Helme
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Vercel News
Vercel News
The Hacker News
The Hacker News
Y
Y Combinator Blog
Latest news
Latest news
SecWiki News
SecWiki News
Hugging Face - Blog
Hugging Face - Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cisco Blogs
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
宝玉的分享
宝玉的分享
L
LINUX DO - 热门话题

博客园 - 华安

Unity 相机:正交 (Orthographic) vs 透视 (Perspective) - 华安 unity中的button中的onclick控制面板中四个参数的意思分别是什么 - 华安 微信小程序开发中触发 onshow的几种特殊情况 SQL Server备份心得 MYSQL 备份数据库 微信与支付支付功能开发 微信小程序中页面配置下拉刷新 unity中 相机没有视锥效果线框了,如何打开 JSONPath表达式 C# 中的操作JSON类 JObject cookie中的 HttpOnly 、Secure、SameSite 解释 Spring-boot 中基于 IP 的限流和自动封禁 Filter 登录 用 HMAC-SHA256 实现 TwoFA(二重验证)的坑 利用Spring Boot的 filter 结合ConcurrentHashMap 实现“同一IP每分钟最多允许300个404请求,超出后禁用30分钟访问” pixi-filters中的BackdropBlurFilter使用注意事项 PixiJS中的 SplitBitmapText.chars中的每个 BitmapText的x值分析 legend隐藏不想显示的图例 springboot获取post请求参数 Javascript如何判断是触摸屏还是PC端 JavaScript获取鼠标点一个元素,获取鼠标点击的元素内的位置 spring-boot中配置Mongodbd的问题小结 Rest Template中添加 PATCH请求。 CSS实现修改CheckBox样式 SpringBoot+Thyemleaf报错:Error resolving template Template might not exist or might not be accessible div display flex 如何出现横向滚动条
spring-boot HttpServletResponse response.sendRedirect是会跳转到 http而不是https
华安 · 2025-12-19 · via 博客园 - 华安

问题原因分析

Spring Boot 的 sendRedirect 方法在通过 Nginx 代理时,会因缺少协议头信息而默认跳转到 HTTP,导致 HTTPS 请求被重定向到 HTTP,从而引发安全问题。具体原因如下:

  1. ‌Nginx 代理机制‌:Nginx 将 HTTPS 请求转发给 Spring Boot 时,仅传递 HTTP 协议头(X-Forwarded-Proto: http),而 Spring Boot 未识别该头,导致重定向时协议丢失。
  2. ‌Spring Boot 缺乏协议感知‌:默认情况下,Spring Boot 无法识别 Nginx 传递的协议头(如 X-Forwarded-Proto),导致重定向时协议被重置为 HTTP。

解决方案

1. ‌Nginx 配置调整‌

在 Nginx 配置中添加协议头并启用重定向转换:

server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
    
    location / {
        proxy_pass http://localhost:8080;  # Spring Boot 端口
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;  # 关键:传递协议头
        proxy_redirect http:// https://;  # 自动转换重定向协议
    }
}

‌作用‌:

  • proxy_set_header X-Forwarded-Proto $scheme;:将原始协议(HTTPS)传递给 Spring Boot。
  • proxy_redirect http:// https://;:自动将 Spring Boot 返回的 HTTP 重定向转换为 HTTPS。

2. ‌Spring Boot 配置启用协议感知‌

在 application.properties 中启用协议头处理:

server.forward-headers-strategy=framework
server.tomcat.remote-ip-header=X-Forwarded-For
server.tomcat.protocol-header=X-Forwarded-Proto

作用‌:

  • forward-headers-strategy=framework:启用 Spring Boot 内置的协议头处理机制。
  • protocol-header=X-Forwarded-Proto:指定协议头名称(需与 Nginx 一致)。

3. ‌可选:自定义重定向过滤器‌

若上述配置无效,可添加过滤器强制 HTTPS:

import org.springframework.stereotype.Component;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class HttpsRedirectFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        String scheme = httpRequest.getHeader("X-Forwarded-Proto");
        if ("http".equals(scheme)) {
            String redirectUrl = "https://" + httpRequest.getServerName() + httpRequest.getRequestURI();
            httpResponse.sendRedirect(redirectUrl);
        } else {
            chain.doFilter(request, response);
        }
    }
}

‌作用‌:

  • 检查 X-Forwarded-Proto 头,若为 HTTP 则强制重定向到 HTTPS。

关键配置说明

  1. ‌Nginx 必须配置‌:
    • proxy_set_header X-Forwarded-Proto $scheme;:传递原始协议头。
    • proxy_redirect http:// https://;:自动转换重定向协议。
  2. ‌Spring Boot 必须配置‌:
    • server.forward-headers-strategy=framework:启用协议头处理。
    • server.tomcat.protocol-header=X-Forwarded-Proto:指定协议头名称。
  3. ‌验证步骤‌:
    • 启动应用后,访问 HTTPS 页面,检查重定向 URL 是否为 HTTPS。
    • 若仍跳转 HTTP,检查 Nginx 配置是否生效(如 proxy_redirect 是否启用)。

‌总结‌:通过 Nginx 传递协议头并启用 Spring Boot 的协议感知机制,可确保 sendRedirect 方法自动跳转到 HTTPS。若配置无效,可添加自定义过滤器强制 HTTPS。