dropwizard/dropwizard-web

GitHub: dropwizard/dropwizard-web

为 Dropwizard 应用提供声明式的 HTTP 安全头部配置,通过 YAML 文件快速启用 HSTS、CSP、CORS 等安全策略。

Stars: 14 | Forks: 6

# dropwizard-web [![Build Status](https://static.pigsec.cn/wp-content/uploads/repos/2026/03/d27a2e01ca100059.svg)](https://github.com/dropwizard/dropwizard-web/actions) [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=dropwizard_dropwizard-web&metric=coverage)](https://sonarcloud.io/summary/new_code?id=dropwizard_dropwizard-web) [![Maven Central](https://img.shields.io/maven-central/v/io.dropwizard.modules/dropwizard-web.svg)](http://mvnrepository.com/artifact/io.dropwizard.modules/dropwizard-web) 为配置对 Web 服务至关重要的各种 HTTP 头部提供支持。 ## 支持特性 - HTTP Strict Transport Security (HSTS) - X-Frame-Options - X-Content-Type-Options - X-XSS-Protection (XSS) - Content Security Policy (CSP) - Cross-Origin Resource Sharing (CORS) - 其他自定义头部 ## Dropwizard 版本支持矩阵 | dropwizard-web | Dropwizard v1.3.x | Dropwizard v2.0.x | Dropwizard v2.1.x | Dropwizard v3.0.x | Dropwizard v4.0.x | |----------------|--------------------|--------------------|--------------------|--------------------|--------------------| | v1.3.x | :white_check_mark: | :white_check_mark: | :question: | :x: | :x: | | v1.4.x | :white_check_mark: | :white_check_mark: | :question: | :x: | :x: | | v1.5.x | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :x: | | v2.0.x | :x: | :x: | :x: | :white_check_mark: | :question: | | v3.0.x | :x: | :x: | :x: | :white_check_mark: | :white_check_mark: | ## 用法 在你的应用程序的 `Configuration` 类中,添加一个 `WebConfiguration` 对象: ``` public class ExampleConfiguration extends Configuration { ... @Valid @NotNull @JsonProperty("web") private WebConfiguration webConfiguration = new WebConfiguration(); public WebConfiguration getWebConfiguration() { return webConfiguration; } public void setWebConfiguration(final WebConfiguration webConfiguration) { this.webConfiguration = webConfiguration; } } ``` 在你的 `initialize` 方法中,向 `Boostrap` 对象添加一个 `WebBundle`: ``` bootstrap.addBundle(new WebBundle<>() { @Override public WebConfiguration getWebConfiguration(final ExampleConfiguration configuration) { return configuration.getWebConfiguration(); } // Optional: Override Servlet environment to apply the configuration to the admin servlets @Override protected ServletEnvironment getServletEnvironment(Environment environment) { return environment.admin(); } }); ``` ## 基本配置 在你的 `config.yml` 文件中定义以下配置: ``` web: uriPath: /api hsts: enabled: true frame-options: enabled: true content-type-options: enabled: true xss-protection: enabled: true ``` `uriPath` 应指明 API 提供服务的路径。 此最小配置会产生以下结果: - HSTS 配置为 1 年,包括子域 - 禁用帧 (Frames) - 禁用 Content-Type 探测 - 开启 XSS 过滤并处于 `block` 模式 支持 CORS 或 CSP 需要额外配置。 ## Maven 构件 此项目可在 Maven Central 上获取。要将其添加到你的项目中,只需将以下依赖项添加到你的 `pom.xml` 中: ``` io.dropwizard.modules dropwizard-web ${dropwizard-web.version} ``` ## 配置参考 ### Web 配置 Name | Default | Description ---- | ------- | ----------- hsts | (1 year, including sub domains) | 配置 Strict-Transport-Security。 frame-options | (disable frames) | 配置 X-Frame-Options。 content-type-options | (disable content-type sniffing) | 配置 X-Content-Type-Options。 xss-protection | (on in block mode) | 配置 X-XSS-Protection。 csp | (none) | 配置 Content Security Policy。 cors | (none) | 配置 Cross-Origin Resource Sharing。 headers | (none) | 配置自定义头部。 ### HTTP Strict Transport Security (HSTS) https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security ``` web: hsts: enabled: true maxAge: 365 days includeSubDomains: true ``` Name | Default | Description ---- | ------- | ----------- maxAge | 365 days | 浏览器应记住该站点仅可通过 HTTPS 访问的时间。 includeSubDomains | true | 如果为 `true`,此规则也适用于该站点的所有子域。 preload | false | 详见 [Preloading Strict Transport Security](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security#Preloading_Strict_Transport_Security)。 enabled | false | 如果为 false,则不应用头部。 ### X-Frame-Options https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options ``` web: frame-options: enabled: true option: SAMEORIGIN ``` Name | Default | Description ---- | ------- | ----------- option | DENY | 必须是以下之一:DENY, SAMEORIGIN, ALLOW-FROM origin | (none) | 如果选项为 ALLOW-FROM,则标识允许在帧中显示此页面的来源。 enabled | false | 如果为 false,则不应用头部。 ### X-Content-Type-Options https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options ``` web: content-type-options: enabled: true ``` Name | Default | Description ---- | ------- | ----------- enabled | false | 如果为 false,则不应用头部。 ### X-XSS-Protection (XSS) https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection ``` web: xss-protection: enabled: true on: true block: true ``` Name | Default | Description ---- | ------- | ----------- on | true | 如果为 true,则启用 XSS 过滤。 block | true | 如果为 true,当浏览器检测到攻击时,将不会渲染该页面。如果为 false,浏览器将清理页面以移除不安全的部分。 enabled | false | 如果为 false,则不应用头部。 ### Content Security Policy (CSP) - https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only ``` web: csp: enabled: true policy: "default-src 'self'" ``` Name | Default | Description ---- | ------- | ----------- policy | (none) | 控制浏览器允许为页面加载的资源的策略指令。 reportOnlyPolicy | (none) | 与 'policy' 相同,但仅报告违规行为而不阻止它们。 enabled | false | 如果为 false,则不应用头部。 ### Cross-Origin Resource Sharing (CORS) - https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS - https://www.eclipse.org/jetty/documentation/9.4.x/cross-origin-filter.html ``` web: cors: allowedOrigins: ["example.com"] allowedMethods: ["DELETE","GET","HEAD","POST","PUT"] allowedHeaders: ["Accept","Authorization","Content-Type","Origin","X-Requested-With"] preflightMaxAge: 30 minutes ``` Name | Default | Description ---- | ------- | ----------- allowedOrigins | (all origins) | 允许访问资源的来源列表。 allowedTimingOrigins | (no origins) | 允许对资源进行计时的来源列表。 allowedMethods | ["GET","POST","HEAD"] | 访问资源时允许使用的 HTTP 方法列表。 preflightMaxAge | 30 minutes | 客户端可以缓存预检请求的持续时间。 allowCredentials | true | 一个布尔值,指示资源是否允许带有凭证的请求。 exposedHeaders | (empty list) | 允许在客户端暴露的 HTTP 头部列表。 chainPreflight | true | 如果为 true,预检请求将被链接到其目标资源进行正常处理(作为 OPTION 请求)。否则,过滤器将响应预检。 ### 其他头部 ``` web: headers: X-Custom-Header-1: custom value 1 X-Custom-Header-2: custom value 2 ``` Name | Default | Description ---- | ------- | ----------- headers | (none) | 要包含在响应中的头部映射(名称和值)。 ## 支持 请在 [GitHub issues](https://github.com/dropwizard/dropwizard-web/issues) 中提交错误报告和功能请求。
标签:CISA项目, CORS, CSP, Dropwizard, GitHub Advanced Security, HSTS, HTTP头部配置, JS文件枚举, Web安全, Web服务, X-Frame-Options, XSS防护, 中间件安全, 安全加固, 安全标准, 开源库, 搜索引擎爬虫, 蓝队分析, 防御机制