pub struct CsrfMiddleware { /* private fields */ }Expand description
CSRF protection middleware.
Implements protection against Cross-Site Request Forgery attacks using the double-submit cookie pattern by default.
§How It Works
- For safe methods (GET, HEAD, OPTIONS, TRACE): generates a CSRF token and sets it in a cookie if not present.
- For state-changing methods (POST, PUT, DELETE, PATCH): validates that the token in the header matches the token in the cookie.
§Example
ⓘ
use fastapi_core::middleware::{CsrfMiddleware, CsrfConfig};
let mut stack = MiddlewareStack::new();
stack.push(CsrfMiddleware::new());
// Or with custom configuration:
let csrf = CsrfMiddleware::with_config(
CsrfConfig::new()
.header_name("X-XSRF-Token")
.cookie_name("XSRF-TOKEN")
.production(false)
);
stack.push(csrf);Implementations§
Source§impl CsrfMiddleware
impl CsrfMiddleware
Sourcepub fn with_config(config: CsrfConfig) -> Self
pub fn with_config(config: CsrfConfig) -> Self
Creates a new CSRF middleware with the given configuration.
Trait Implementations§
Source§impl Clone for CsrfMiddleware
impl Clone for CsrfMiddleware
Source§fn clone(&self) -> CsrfMiddleware
fn clone(&self) -> CsrfMiddleware
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 CsrfMiddleware
impl Debug for CsrfMiddleware
Source§impl Default for CsrfMiddleware
impl Default for CsrfMiddleware
Source§impl Middleware for CsrfMiddleware
impl Middleware for CsrfMiddleware
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 CsrfMiddleware
impl RefUnwindSafe for CsrfMiddleware
impl Send for CsrfMiddleware
impl Sync for CsrfMiddleware
impl Unpin for CsrfMiddleware
impl UnwindSafe for CsrfMiddleware
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).