Class SecurityHeaders
- All Implemented Interfaces:
Middleware
app.use(SecurityHeaders.defaults());
app.use(SecurityHeaders.defaults().with("Strict-Transport-Security", "max-age=31536000"));
defaults() sets:
| Header | Value | Effect |
|---|---|---|
X-Content-Type-Options | nosniff | browsers keep the declared media type |
X-Frame-Options | DENY | no framing (older browsers) |
Content-Security-Policy | default-src 'none'; frame-ancestors 'none' |
a response rendered by a browser loads nothing and cannot be framed |
Referrer-Policy | no-referrer | no URL leaks through Referer |
Cross-Origin-Resource-Policy | same-origin | other origins cannot embed responses |
Strict-Transport-Security is not a default: HSTS is only meaningful, and safe to enable,
where the deployment serves HTTPS for the whole host and keeps doing so. Add it with with(String, String) on a TLS listener or behind a TLS-terminating proxy.
A header the response already carries is left unchanged, so a handler can deliberately loosen
one (for example a CSP for an HTML page). Registered globally, the middleware also decorates the
router's own 404, 405, automatic OPTIONS and 501 answers. It also decorates responses mapped from
exceptions (problem responses, including the 401 and 403 of Security policies, and error
handler responses) through Middleware.afterError(com.jsgalactic.axiom.context.Context, com.jsgalactic.axiom.http.Response).
Immutable and thread-safe; one instance can serve every request.
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.jsgalactic.axiom.context.Middleware
Middleware.Next -
Method Summary
Modifier and TypeMethodDescriptionafterError(Context context, Response response) Decorates a response that was produced from an exception: a problem response for anAxiomException, the response of an error handler, the generic 500, or the 406 for an unacceptable response type.static SecurityHeadersdefaults()Returns the default header set described above.handle(Context context, Middleware.Next next) Handles one request, usually by callingnext.run().headers()Returns the headers this middleware sets.Returns a copy that also sets, or replaces, one header.Returns a copy that no longer sets one header.
-
Method Details
-
defaults
Returns the default header set described above.- Returns:
- default security headers
-
with
Returns a copy that also sets, or replaces, one header.- Parameters:
name- header name, an HTTP tokenvalue- header value without control characters- Returns:
- a copy with the header
- Throws:
IllegalArgumentException- for an invalid name or value
-
without
Returns a copy that no longer sets one header.- Parameters:
name- header name, matched case-insensitively- Returns:
- a copy without the header
-
headers
Returns the headers this middleware sets.- Returns:
- immutable, case-insensitive header map
-
handle
Description copied from interface:MiddlewareHandles one request, usually by callingnext.run().- Specified by:
handlein interfaceMiddleware- Parameters:
context- the request-scoped context shared with the handler; status settings made here apply to the handler's result mappingnext- the rest of the chain- Returns:
- the response to use; never null
- Throws:
Exception- to fail the request as a handler exception would
-
afterError
Description copied from interface:MiddlewareDecorates a response that was produced from an exception: a problem response for anAxiomException, the response of an error handler, the generic 500, or the 406 for an unacceptable response type. The default returns the response unchanged.It is called once per such response for each middleware that was entered when the failure happened (a middleware an outer one short-circuited before was never entered), innermost first, after error mapping and before the response leaves
app.handle. It is not called for responses that come back throughMiddleware.Next.run(), including responses a middleware or handler builds itself, nor for the router's 404, 405 and 501 answers (those flow through global middleware as ordinary responses), nor for failures before routing (413, CONNECT) and listener errors. An exception that nothing maps, and a request that was cancelled or timed out, never reach it.Like
Middleware.handle(com.jsgalactic.axiom.context.Context, com.jsgalactic.axiom.context.Middleware.Next), it runs on the request's thread under the request deadline and must be thread-safe. It must not throw: an exception from it is not mapped again. AnAxiomExceptionanswers with its own undecorated problem response and anything else fails the request as an unexpected exception.- Specified by:
afterErrorin interfaceMiddleware- Parameters:
context- the failed request's contextresponse- the response produced from the exception, never null- Returns:
- the response to pass outwards; never null
-