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 属性。以下示例显示了如何操作
spring:
cloud:
gateway:
filter:
secure-headers:
disable: x-frame-options,strict-transport-security
要将 SecureHeaders 过滤器应用于特定路由,请将该过滤器添加到该路由的过滤器列表中。您可以使用参数自定义路由过滤器。路由配置会覆盖此路由的全局默认配置。
- id: secureheaders_route
uri: http://example.org
predicates:
- Path=/**
filters:
- name: SecureHeaders
args:
disable: x-frame-options
| 需要使用安全头的完整小写名称来禁用它。 |
更多选项
spring:
cloud:
gateway:
filter:
secure-headers:
enable: permissions-policy
permissions-policy : geolocation=(self "https://example.com")
在上述示例中,除了自己的来源和来源为“https://example.com”的来源之外,地理定位 API 在所有浏览上下文中均被禁用。可以为每个路由单独配置权限策略。
- 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 的权限策略功能列表。 |
当您为环境配置头值时,请务必检查浏览器控制台是否存在语法错误。