Skip to content

SecureHeaders GatewayFilter 工厂

SecureHeaders GatewayFilter 工厂根据这篇博客文章的建议,向响应中添加多个安全头。

以下头(显示其默认值)会被添加:

  • X-Xss-Protection:1 (mode=block)
  • Strict-Transport-Security (max-age=631138519)
  • X-Frame-Options (DENY)
  • X-Content-Type-Options (nosniff)
  • Referrer-Policy (no-referrer)
  • Content-Security-Policy (default-src 'self' https:; font-src 'self' https: data:; img-src 'self' https: data:; object-src 'none'; script-src https:; style-src 'self' https: 'unsafe-inline')
  • X-Download-Options (noopen)
  • X-Permitted-Cross-Domain-Policies (none)

要更改默认值,请在 spring.cloud.gateway.filter.secure-headers 命名空间中设置相应的属性。 以下属性可用:

  • xss-protection-header
  • strict-transport-security
  • frame-options
  • content-type-options
  • referrer-policy
  • content-security-policy
  • download-options
  • permitted-cross-domain-policies

要禁用默认值,请使用逗号分隔的值设置 spring.cloud.gateway.filter.secure-headers.disable 属性。 以下示例展示了如何操作:

示例 1. application.yml

yaml
spring:
  cloud:
    gateway:
      filter:
        secure-headers:
          disable: x-frame-options,strict-transport-security

要将 SecureHeaders 过滤器应用于特定路由,请将该过滤器添加到该路由的过滤器列表中。 您可以使用参数自定义路由过滤器。路由配置会覆盖此路由的全局默认配置。

示例 2. application.yml

yaml
      - id: secureheaders_route
        uri: http://example.org
        predicates:
        - Path=/**
        filters:
          - name: SecureHeaders
            args:
              disable: x-frame-options

注意: 需要使用安全头的小写全名来禁用它。

更多选项

您可以选择向响应添加 Permissions-Policy 头。Permissions Policy 是一个安全头, 允许 Web 开发人员管理网站可以使用哪些浏览器功能。请参阅 Permissions-Policy指令 为您的环境进行配置。

示例 3. application.yml

yaml
spring:
  cloud:
    gateway:
      filter:
        secure-headers:
          permissions-policy: "geolocation=(self), camera=()"

补充教学 —— 为什么要使用安全头?

场景 1:防止 XSS 攻击(X-Xss-Protection)

  • 攻击方式:攻击者通过在 URL 中注入恶意脚本,当用户访问该 URL 时,脚本会被反射并执行。
  • 防护机制X-Xss-Protection: 1; mode=block 告诉浏览器启用 XSS 过滤器,并在检测到攻击时阻止整个页面渲染。
  • 实际效果
    • 请求:GET /search?q=<script>alert('XSS')</script>
    • 浏览器检测到恶意脚本,阻止页面渲染
    • 用户看到空白页面或错误提示,而不是执行恶意脚本

场景 2:强制 HTTPS(Strict-Transport-Security)

  • 背景:网站同时支持 HTTP 和 HTTPS,用户可能通过 HTTP 访问,导致中间人攻击。
  • 防护机制Strict-Transport-Security: max-age=631138519 告诉浏览器在指定时间内(约 20 年)只通过 HTTPS 访问该网站。
  • 实际效果
    • 用户首次通过 HTTPS 访问后,浏览器记住此规则
    • 后续所有访问(包括用户手动输入 http://)都会自动升级为 HTTPS
    • 防止降级攻击和 SSL 剥离攻击

场景 3:防止点击劫持(X-Frame-Options)

  • 攻击方式:攻击者将你的网站嵌入到恶意网站的 iframe 中,诱导用户点击看似无害的按钮,实际执行敏感操作。
  • 防护机制X-Frame-Options: DENY 完全禁止网站被嵌入到任何 iframe 中。
  • 实际效果
    • 恶意网站:<iframe src="https://yourbank.com/transfer"></iframe>
    • 浏览器拒绝加载 iframe,显示空白或错误
    • 防止用户在不知情的情况下执行转账等敏感操作

场景 4:防止 MIME 类型嗅探(X-Content-Type-Options)

  • 攻击方式:攻击者上传伪装成图片的恶意脚本,浏览器根据内容猜测 MIME 类型,将图片当作脚本执行。
  • 防护机制X-Content-Type-Options: nosniff 告诉浏览器严格遵守服务器声明的 Content-Type,不进行嗅探。
  • 实际效果
    • 服务器声明:Content-Type: image/jpeg
    • 浏览器严格按照图片处理,即使内容包含 JavaScript 代码也不执行
    • 防止文件上传漏洞导致的 XSS 攻击

场景 5:内容安全策略(Content-Security-Policy)

  • 防护机制:CSP 是最强大的 XSS 防护机制,通过白名单限制资源加载来源。
  • 实际效果
    • 默认策略:只允许从当前域名和 HTTPS 加载资源
    • 阻止内联脚本:<script>alert('XSS')</script> 不会执行
    • 阻止外部恶意脚本:只能加载白名单中的 CDN
    • 防止数据泄露:限制图片、字体、样式等资源的加载来源

场景 6:控制浏览器功能(Permissions-Policy)

  • 防护机制:控制网站可以使用哪些浏览器功能,如地理位置、摄像头、麦克风等。
  • 实际效果
    • 配置:permissions-policy: "geolocation=(self), camera=()"
    • 允许当前域名访问地理位置
    • 完全禁止访问摄像头
    • 防止恶意网站滥用用户设备功能

场景 7:控制引用信息泄露(Referrer-Policy)

  • 背景:用户从敏感页面(如银行账户)跳转到外部链接时,URL 中的敏感信息会通过 Referer 头泄露。
  • 防护机制Referrer-Policy: no-referrer 完全不发送 Referer 信息。
  • 实际效果
    • 用户从 https://bank.com/account?user=123 跳转到外部链接
    • 外部网站收到的 Referer 为空
    • 防止敏感参数(如用户 ID、订单号)泄露给第三方

场景 8:禁用特定安全头(灵活配置)

  • 场景:某些旧版浏览器或特殊业务需求需要禁用某些安全头。
  • 配置方式
    • 全局禁用:在 spring.cloud.gateway.filter.secure-headers.disable 中配置
    • 路由级别禁用:在特定路由的过滤器参数中配置
    • 路由配置会覆盖全局配置
  • 示例
    • 需要在 iframe 中嵌入页面时,禁用 X-Frame-Options
    • 开发环境禁用 Strict-Transport-Security 以支持 HTTP
    • 生产环境启用所有安全头,提供最大保护

Based on Spring Framework.