pub struct SecurityHeaders { /* private fields */ }Expand description
Middleware that adds security-related HTTP headers to responses.
This middleware helps protect against common web vulnerabilities by setting appropriate security headers. It’s recommended for all web applications.
§Headers
- X-Content-Type-Options: Prevents MIME type sniffing
- X-Frame-Options: Controls iframe embedding (clickjacking protection)
- X-XSS-Protection: Legacy XSS filter control (disabled by default)
- Content-Security-Policy: Controls resource loading
- Strict-Transport-Security: Enforces HTTPS
- Referrer-Policy: Controls referrer information
- Permissions-Policy: Controls browser features
§Example
ⓘ
use fastapi_core::middleware::{SecurityHeaders, SecurityHeadersConfig};
// Use defaults
let mw = SecurityHeaders::new();
// Custom configuration
let config = SecurityHeadersConfig::default()
.content_security_policy("default-src 'self'; img-src *")
.hsts(86400, false, false); // 1 day
let mw = SecurityHeaders::with_config(config);Implementations§
Source§impl SecurityHeaders
impl SecurityHeaders
Sourcepub fn with_config(config: SecurityHeadersConfig) -> Self
pub fn with_config(config: SecurityHeadersConfig) -> Self
Creates a new middleware with custom configuration.
Trait Implementations§
Source§impl Clone for SecurityHeaders
impl Clone for SecurityHeaders
Source§fn clone(&self) -> SecurityHeaders
fn clone(&self) -> SecurityHeaders
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for SecurityHeaders
impl Debug for SecurityHeaders
Source§impl Default for SecurityHeaders
impl Default for SecurityHeaders
Source§impl Middleware for SecurityHeaders
impl Middleware for SecurityHeaders
Source§fn after<'a>(
&'a self,
_ctx: &'a RequestContext,
_req: &'a Request,
response: Response,
) -> BoxFuture<'a, Response>
fn after<'a>( &'a self, _ctx: &'a RequestContext, _req: &'a Request, response: Response, ) -> BoxFuture<'a, Response>
Called after the handler executes. Read more
Source§fn name(&self) -> &'static str
fn name(&self) -> &'static str
Returns the middleware name for debugging and logging. Read more
Source§fn before<'a>(
&'a self,
_ctx: &'a RequestContext,
_req: &'a mut Request,
) -> BoxFuture<'a, ControlFlow>
fn before<'a>( &'a self, _ctx: &'a RequestContext, _req: &'a mut Request, ) -> BoxFuture<'a, ControlFlow>
Called before the handler executes. Read more
Auto Trait Implementations§
impl Freeze for SecurityHeaders
impl RefUnwindSafe for SecurityHeaders
impl Send for SecurityHeaders
impl Sync for SecurityHeaders
impl Unpin for SecurityHeaders
impl UnwindSafe for SecurityHeaders
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).