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 屬性。以下示例顯示瞭如何操作

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

要將 SecureHeaders 過濾器應用於特定路由,請將該過濾器新增到該路由的過濾器列表中。您可以使用引數自定義路由過濾器。路由配置會覆蓋此路由的全域性預設配置。

application.yml
      - id: secureheaders_route
        uri: http://example.org
        predicates:
        - Path=/**
        filters:
          - name: SecureHeaders
            args:
              disable: x-frame-options
需要使用安全頭的完整小寫名稱來停用它。

更多選項

您可以選擇將 Permissions-Policy 頭新增到響應中。許可權策略是一種安全頭,允許 Web 開發人員管理網站可以使用哪些瀏覽器功能。請參閱許可權策略指令,以便為您的環境進行配置。

application.yml
spring:
  cloud:
    gateway:
      filter:
        secure-headers:
          enable: permissions-policy
          permissions-policy : geolocation=(self "https://example.com")

在上述示例中,除了自己的來源和來源為“https://example.com”的來源之外,地理定位 API 在所有瀏覽上下文中均被停用。可以為每個路由單獨配置許可權策略。

application.yml
      - id: secureheaders_route
        uri: http://anotherexample.org
        predicates:
        - Path=/**
        filters:
          - name: SecureHeaders
            args:
              disable: x-frame-options
              enable: permissions-policy
              permissions-policy : geolocation=("https://anotherexample.org")
當您啟用許可權策略並且未明確配置任何指令時,將應用預設值。具體來說,此預設值停用了各種標準化和實驗性功能。此行為可能不適合您的特定環境或用例。

啟用且未明確配置時的許可權策略預設值

Permissions-Policy: accelerometer=(), ambient-light-sensor=(), autoplay=(), battery=(), camera=(), cross-origin-isolated=(), display-capture=(), document-domain=(), encrypted-media=(), execution-while-not-rendered=(), execution-while-out-of-viewport=(), fullscreen=(), geolocation=(), gyroscope=(), keyboard-map=(), magnetometer=(), microphone=(), midi=(), navigation-override=(), payment=(), picture-in-picture=(), publickey-credentials-get=(), screen-wake-lock=(), sync-xhr=(), usb=(), web-share=(), xr-spatial-tracking=()

您可以使用開發者工具整合檢視 Chrome 的許可權策略功能列表。

當您為環境配置頭值時,請務必檢查瀏覽器控制檯是否存在語法錯誤。

© . This site is unofficial and not affiliated with VMware.