Class SecurityHeaders
Provides a builder-pattern API for constructing a set of HTTP security response headers. Supports modern best-practice headers including Content-Security-Policy, Strict-Transport-Security, X-Frame-Options, and Cross-Origin policies.
提供构建器模式 API 用于构建一组 HTTP 安全响应头部。支持现代最佳实践头部, 包括 Content-Security-Policy、Strict-Transport-Security、X-Frame-Options 和跨域策略。
Features | 主要功能:
- Fluent builder API for all security headers - 所有安全头部的流式构建器 API
- Preset configurations (strict, standard) - 预设配置(严格、标准)
- Apply to HttpHeaders or export as Map - 应用到 HttpHeaders 或导出为 Map
- Type-safe enums for X-Frame-Options and Referrer-Policy - X-Frame-Options 和 Referrer-Policy 类型安全枚举
Usage Examples | 使用示例:
// Strict preset
SecurityHeaders headers = SecurityHeaders.strict();
// Standard preset
SecurityHeaders headers = SecurityHeaders.standard();
// Custom configuration
SecurityHeaders headers = SecurityHeaders.builder()
.contentSecurityPolicy("default-src 'self'")
.strictTransportSecurity(31536000, true)
.xFrameOptions(SecurityHeaders.FrameOption.DENY)
.xContentTypeOptions()
.xXssProtection()
.referrerPolicy(SecurityHeaders.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN)
.build();
// Apply to HttpHeaders
HttpHeaders httpHeaders = HttpHeaders.of();
headers.applyTo(httpHeaders);
// Export as Map
Map<String, String> map = headers.toMap();
Security | 安全性:
- Thread-safe: Yes (immutable) - 是(不可变)
- Null-safe: Builder rejects null arguments - 构建器拒绝 null 参数
- Since:
- JDK 25, opencode-base-web V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for SecurityHeaders - fluent API for constructing security header sets.static enumX-Frame-Options header values.static enumReferrer-Policy header values. -
Method Summary
Modifier and TypeMethodDescriptionvoidapplyTo(HttpHeaders httpHeaders) Applies all security headers to the given HttpHeaders.static SecurityHeaders.Builderbuilder()Creates a new builder.static SecurityHeadersstandard()Creates a standard security headers configuration suitable for most applications.static SecurityHeadersstrict()Creates a strict security headers configuration with the most restrictive settings.toMap()Returns an unmodifiable map of all security headers.toString()
-
Method Details
-
builder
Creates a new builder. 创建新的构建器。- Returns:
- a new Builder - 新的构建器
-
strict
Creates a strict security headers configuration with the most restrictive settings. 创建具有最严格设置的安全头部配置。Includes: DENY frame options, nosniff, no-referrer, strict CSP, HSTS with 1 year max-age and includeSubDomains, XSS protection disabled (modern best practice), and strict cross-origin policies.
包含:DENY 框架选项、nosniff、no-referrer、严格 CSP、 1 年 max-age 和 includeSubDomains 的 HSTS、禁用 XSS 保护(现代最佳实践)、 以及严格的跨域策略。
- Returns:
- strict SecurityHeaders - 严格的 SecurityHeaders
-
standard
Creates a standard security headers configuration suitable for most applications. 创建适用于大多数应用程序的标准安全头部配置。Includes: SAMEORIGIN frame options, nosniff, strict-origin-when-cross-origin referrer, HSTS with 1 year max-age, and XSS protection disabled (modern best practice).
包含:SAMEORIGIN 框架选项、nosniff、strict-origin-when-cross-origin referrer、 1 年 max-age 的 HSTS、以及禁用 XSS 保护(现代最佳实践)。
- Returns:
- standard SecurityHeaders - 标准的 SecurityHeaders
-
applyTo
Applies all security headers to the given HttpHeaders. 将所有安全头部应用到给定的 HttpHeaders。- Parameters:
httpHeaders- the headers to apply to - 要应用的头部
-
toMap
-
toString
-