Class SecurityHeaders
SecurityHeaders provides a centralized way to configure standard security headers
that are applied to every HTTP response. This complements ContentSecurityPolicy
and CacheControl by covering the remaining OWASP-recommended headers.
Usage:
// In Application.initialize():
setSecurityHeaders(SecurityHeaders.production());
// Or customize:
setSecurityHeaders(new SecurityHeaders()
.frameOptions(FrameOption.SAMEORIGIN)
.hstsMaxAge(31536000)
.referrerPolicy(ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN)
.permissionsPolicy("camera=(), microphone=(), geolocation=()"));
- Since:
- 2.1
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumOptions for theX-Frame-Optionsheader.static enumOptions for theReferrer-Policyheader. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidapply(OorianHttpResponse response) Applies all configured security headers to the given HTTP response.Adds theX-Content-Type-Options: nosniffheader.crossOriginOpenerPolicy(String policy) Sets theCross-Origin-Opener-Policyheader.crossOriginResourcePolicy(String policy) Sets theCross-Origin-Resource-Policyheader.Sets theX-Frame-Optionsheader.static SecurityHeadersReturns the global SecurityHeaders instance.Sets a custom security header.hstsMaxAge(long maxAgeSeconds) Sets theStrict-Transport-Securityheader (HSTS).hstsMaxAge(long maxAgeSeconds, boolean includeSubDomains) Sets theStrict-Transport-Securityheader withincludeSubDomains.permissionsPolicy(String policy) Sets thePermissions-Policyheader.static SecurityHeadersCreates a production-ready configuration with secure defaults.Sets theReferrer-Policyheader.
-
Constructor Details
-
SecurityHeaders
public SecurityHeaders()Creates an empty SecurityHeaders configuration.
-
-
Method Details
-
production
Creates a production-ready configuration with secure defaults.Includes:
X-Content-Type-Options: nosniffX-Frame-Options: DENYReferrer-Policy: strict-origin-when-cross-originPermissions-Policy: camera=(), microphone=(), geolocation=()Cross-Origin-Opener-Policy: same-originCross-Origin-Resource-Policy: same-origin
- Returns:
- a SecurityHeaders instance with production defaults
-
getInstance
Returns the global SecurityHeaders instance.- Returns:
- the instance, or
nullif not configured
-
contentTypeOptions
Adds theX-Content-Type-Options: nosniffheader.Prevents browsers from MIME-sniffing the content type, which can prevent XSS attacks via content-type confusion.
- Returns:
- this instance for chaining
-
frameOptions
Sets theX-Frame-Optionsheader.Controls whether the page can be loaded in frames, preventing clickjacking.
- Parameters:
option- the frame option (DENY or SAMEORIGIN)- Returns:
- this instance for chaining
-
hstsMaxAge
Sets theStrict-Transport-Securityheader (HSTS).Instructs browsers to only access the site via HTTPS for the specified duration. This should only be enabled when the site is served over HTTPS.
- Parameters:
maxAgeSeconds- the time in seconds that the browser should remember to use HTTPS- Returns:
- this instance for chaining
-
hstsMaxAge
Sets theStrict-Transport-Securityheader withincludeSubDomains.- Parameters:
maxAgeSeconds- the HSTS max-age in secondsincludeSubDomains- whether to apply HSTS to all subdomains- Returns:
- this instance for chaining
-
referrerPolicy
Sets theReferrer-Policyheader.Controls how much referrer information is sent with requests. This helps prevent leaking sensitive URL parameters to third-party sites.
- Parameters:
policy- the referrer policy- Returns:
- this instance for chaining
-
permissionsPolicy
Sets thePermissions-Policyheader.Restricts which browser features (camera, microphone, geolocation, etc.) the page may use. Features set to
()are disabled entirely.- Parameters:
policy- the permissions policy string- Returns:
- this instance for chaining
-
crossOriginOpenerPolicy
Sets theCross-Origin-Opener-Policyheader.Controls whether the page can interact with cross-origin windows. A value of
"same-origin"isolates the browsing context group.- Parameters:
policy- the COOP value (e.g.,"same-origin","same-origin-allow-popups")- Returns:
- this instance for chaining
-
crossOriginResourcePolicy
Sets theCross-Origin-Resource-Policyheader.Controls which origins can load this resource. A value of
"same-origin"restricts to same-origin only.- Parameters:
policy- the CORP value (e.g.,"same-origin","same-site","cross-origin")- Returns:
- this instance for chaining
-
header
Sets a custom security header.- Parameters:
name- the header namevalue- the header value- Returns:
- this instance for chaining
-
apply
Applies all configured security headers to the given HTTP response.- Parameters:
response- the HTTP response to add headers to
-