[{"data":1,"prerenderedAt":535},["ShallowReactive",2],{"$fNdR0ivNmNv68aC5LX5b6YbvMgwY77ge9P3Y2olJB0FU":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":521,"editUrl":529,"prev":530,"next":533},"overview\u002Fguards","Guards","Authorize requests with the Guard trait and","---\ntitle: Guards\ndescription: Authorize requests with the Guard trait and #[use_guards].\n---\n\n# Guards\n\nGuards decide **whether a request may continue**. They run after middleware and before interceptors. A guard returns a boolean — `true` allows the request to proceed, `false` rejects it with an unauthorized response.\n\n## The `Guard` trait\n\n```rust\nuse ferrite_framework::{Guard, RequestCtx, async_trait};\n\npub struct AdminGuard;\n\n#[async_trait]\nimpl Guard for AdminGuard {\n    async fn can_activate(&self, ctx: &RequestCtx) -> bool {\n        \u002F\u002F inspect the request (headers, query, state...) and decide\n        ctx.header_str(\"X-Admin-Token\").is_some()\n    }\n}\n```\n\nThe rejection response is generated by the pipeline automatically.\n\n## Applying guards\n\n```rust\nuse ferrite_framework::{use_guards, get, Json, HttpError};\n\n#[impl_controller]\nimpl UsersController {\n    #[use_guards(AdminGuard)]\n    #[get(\"\u002F{id}\u002Fadmin\")]\n    pub async fn admin(&self, Path(id): Path\u003Ci64>) -> Result\u003CJson\u003CUser>, HttpError> { \u002F* ... *\u002F }\n}\n```\n\nMultiple guards are allowed — all of them must pass:\n\n```rust\n#[use_guards(AuthGuard, AdminGuard)]\n```\n\nGuards can also be applied to an entire controller by annotating the impl block:\n\n```rust\n#[impl_controller]\n#[use_guards(AuthGuard)]\nimpl UsersController { \u002F* ... *\u002F }\n```\n\n## Guard order\n\n`Middleware → **Guards** → Interceptors(before) → Pipes → Handler`\n\nGuards never see the transformed body; they inspect the raw `RequestCtx`. Chain multiple guards left-to-right — they are evaluated in declaration order and any failure short-circuits.\n\n## Built-in guards\n\n- `ferrite-auth-jwt` ships `AuthGuard`, which verifies a `Authorization: Bearer \u003Ctoken>` HS256 token. See [Authentication (JWT)](\u002Fdocs\u002Ftechniques\u002Fauthentication).\n- `ferrite-throttler` ships `ThrottlerGuard`, which rate limits by IP\u002Ftoken. See [Rate limiting](\u002Fdocs\u002Ftechniques\u002Frate-limiting).\n\n## Notes\n\n- Guards that need dependencies use `#[injectable]` + `#[inject]` like any provider.\n- Guard metadata (`guard_types`) is recorded per-route and consumed by `ferrite-swagger` to add `401` + security schemes to generated specs.\n\n## Next steps\n\n- [Interceptors](\u002Fdocs\u002Foverview\u002Finterceptors).\n- [Exception filters](\u002Fdocs\u002Foverview\u002Fexception-filters).",{"frontmatter":9,"meta":10,"nodes":34},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,22,25,28,31],{"id":17,"depth":13,"text":18},"the-guard-trait","The Guard trait",{"id":20,"depth":13,"text":21},"applying-guards","Applying guards",{"id":23,"depth":13,"text":24},"guard-order","Guard order",{"id":26,"depth":13,"text":27},"built-in-guards","Built-in guards",{"id":29,"depth":13,"text":30},"notes","Notes",{"id":32,"depth":13,"text":33},"next-steps","Next steps",[35,39,57,65,231,234,236,372,375,393,396,425,427,432,438,440,475,477,505,507],[36,37,5],"h1",{"id":38},"guards",[40,41,42,43,47,48,52,53,56],"p",{},"Guards decide ",[44,45,46],"strong",{},"whether a request may continue",". They run after middleware and before interceptors. A guard returns a boolean — ",[49,50,51],"code",{},"true"," allows the request to proceed, ",[49,54,55],{},"false"," rejects it with an unauthorized response.",[58,59,60,61,64],"h2",{"id":17},"The ",[49,62,63],{},"Guard"," trait",[66,67,70],"pre",{"language":68,"class":69},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[49,71,73,104,105,104,107,104,121,104,123,104,128,104,144,104,187,104,193,104,221,104,226],{"class":72},"language-rust",[74,75,78,82,86,89,93,95,98,101],"span",{"class":76,"style":77},"line","display: inline",[74,79,81],{"style":80},"color:#D73A49;--shiki-dark:#F97583","use",[74,83,85],{"style":84},"color:#6F42C1;--shiki-dark:#B392F0"," ferrite_framework",[74,87,88],{"style":80},"::",[74,90,92],{"style":91},"color:#24292E;--shiki-dark:#E1E4E8","{",[74,94,63],{"style":84},[74,96,97],{"style":91},", ",[74,99,100],{"style":84},"RequestCtx",[74,102,103],{"style":91},", async_trait};","\n",[74,106],{"class":76,"style":77},[74,108,109,112,115,118],{"class":76,"style":77},[74,110,111],{"style":80},"pub",[74,113,114],{"style":80}," struct",[74,116,117],{"style":84}," AdminGuard",[74,119,120],{"style":91},";",[74,122],{"class":76,"style":77},[74,124,125],{"class":76,"style":77},[74,126,127],{"style":91},"#[async_trait]",[74,129,130,133,136,139,141],{"class":76,"style":77},[74,131,132],{"style":80},"impl",[74,134,135],{"style":84}," Guard",[74,137,138],{"style":80}," for",[74,140,117],{"style":84},[74,142,143],{"style":91}," {",[74,145,146,149,152,155,158,161,165,168,171,174,176,179,182,185],{"class":76,"style":77},[74,147,148],{"style":80},"    async",[74,150,151],{"style":80}," fn",[74,153,154],{"style":84}," can_activate",[74,156,157],{"style":91},"(",[74,159,160],{"style":80},"&",[74,162,164],{"style":163},"color:#005CC5;--shiki-dark:#79B8FF","self",[74,166,167],{"style":91},", ctx",[74,169,170],{"style":80},":",[74,172,173],{"style":80}," &",[74,175,100],{"style":84},[74,177,178],{"style":91},") ",[74,180,181],{"style":80},"->",[74,183,184],{"style":84}," bool",[74,186,143],{"style":91},[74,188,189],{"class":76,"style":77},[74,190,192],{"style":191},"color:#6A737D;--shiki-dark:#6A737D","        \u002F\u002F inspect the request (headers, query, state...) and decide",[74,194,195,198,201,204,206,210,213,215,218],{"class":76,"style":77},[74,196,197],{"style":91},"        ctx",[74,199,200],{"style":80},".",[74,202,203],{"style":84},"header_str",[74,205,157],{"style":91},[74,207,209],{"style":208},"color:#032F62;--shiki-dark:#9ECBFF","\"X-Admin-Token\"",[74,211,212],{"style":91},")",[74,214,200],{"style":80},[74,216,217],{"style":84},"is_some",[74,219,220],{"style":91},"()",[74,222,223],{"class":76,"style":77},[74,224,225],{"style":91},"    }",[74,227,228],{"class":76,"style":77},[74,229,230],{"style":91},"}",[40,232,233],{},"The rejection response is generated by the pipeline automatically.",[58,235,21],{"id":20},[66,237,238],{"language":68,"class":69},[49,239,240,104,262,104,264,104,269,104,278,104,289,104,299,104,368],{"class":72},[74,241,242,244,246,248,251,254,256,259],{"class":76,"style":77},[74,243,81],{"style":80},[74,245,85],{"style":84},[74,247,88],{"style":80},[74,249,250],{"style":91},"{use_guards, get, ",[74,252,253],{"style":84},"Json",[74,255,97],{"style":91},[74,257,258],{"style":84},"HttpError",[74,260,261],{"style":91},"};",[74,263],{"class":76,"style":77},[74,265,266],{"class":76,"style":77},[74,267,268],{"style":91},"#[impl_controller]",[74,270,271,273,276],{"class":76,"style":77},[74,272,132],{"style":80},[74,274,275],{"style":84}," UsersController",[74,277,143],{"style":91},[74,279,280,283,286],{"class":76,"style":77},[74,281,282],{"style":91},"    #[use_guards(",[74,284,285],{"style":84},"AdminGuard",[74,287,288],{"style":91},")]",[74,290,291,294,297],{"class":76,"style":77},[74,292,293],{"style":91},"    #[get(",[74,295,296],{"style":208},"\"\u002F{id}\u002Fadmin\"",[74,298,288],{"style":91},[74,300,301,304,307,309,312,314,316,318,320,323,326,328,331,334,337,340,342,345,347,349,351,354,357,359,362,365],{"class":76,"style":77},[74,302,303],{"style":80},"    pub",[74,305,306],{"style":80}," async",[74,308,151],{"style":80},[74,310,311],{"style":84}," admin",[74,313,157],{"style":91},[74,315,160],{"style":80},[74,317,164],{"style":163},[74,319,97],{"style":91},[74,321,322],{"style":84},"Path",[74,324,325],{"style":91},"(id)",[74,327,170],{"style":80},[74,329,330],{"style":84}," Path",[74,332,333],{"style":91},"\u003C",[74,335,336],{"style":84},"i64",[74,338,339],{"style":91},">) ",[74,341,181],{"style":80},[74,343,344],{"style":84}," Result",[74,346,333],{"style":91},[74,348,253],{"style":84},[74,350,333],{"style":91},[74,352,353],{"style":84},"User",[74,355,356],{"style":91},">, ",[74,358,258],{"style":84},[74,360,361],{"style":91},"> { ",[74,363,364],{"style":191},"\u002F* ... *\u002F",[74,366,367],{"style":91}," }",[74,369,370],{"class":76,"style":77},[74,371,230],{"style":91},[40,373,374],{},"Multiple guards are allowed — all of them must pass:",[66,376,377],{"language":68,"class":69},[49,378,379],{"class":72},[74,380,381,384,387,389,391],{"class":76,"style":77},[74,382,383],{"style":91},"#[use_guards(",[74,385,386],{"style":84},"AuthGuard",[74,388,97],{"style":91},[74,390,285],{"style":84},[74,392,288],{"style":91},[40,394,395],{},"Guards can also be applied to an entire controller by annotating the impl block:",[66,397,398],{"language":68,"class":69},[49,399,400,104,404,104,412],{"class":72},[74,401,402],{"class":76,"style":77},[74,403,268],{"style":91},[74,405,406,408,410],{"class":76,"style":77},[74,407,383],{"style":91},[74,409,386],{"style":84},[74,411,288],{"style":91},[74,413,414,416,418,421,423],{"class":76,"style":77},[74,415,132],{"style":80},[74,417,275],{"style":84},[74,419,420],{"style":91}," { ",[74,422,364],{"style":191},[74,424,367],{"style":91},[58,426,24],{"id":23},[40,428,429],{},[49,430,431],{},"Middleware → **Guards** → Interceptors(before) → Pipes → Handler",[40,433,434,435,437],{},"Guards never see the transformed body; they inspect the raw ",[49,436,100],{},". Chain multiple guards left-to-right — they are evaluated in declaration order and any failure short-circuits.",[58,439,27],{"id":26},[441,442,443,462],"ul",{},[444,445,446,449,450,452,453,456,457,200],"li",{},[49,447,448],{},"ferrite-auth-jwt"," ships ",[49,451,386],{},", which verifies a ",[49,454,455],{},"Authorization: Bearer \u003Ctoken>"," HS256 token. See ",[458,459,461],"a",{"href":460},"\u002Fdocs\u002Ftechniques\u002Fauthentication","Authentication (JWT)",[444,463,464,449,467,470,471,200],{},[49,465,466],{},"ferrite-throttler",[49,468,469],{},"ThrottlerGuard",", which rate limits by IP\u002Ftoken. See ",[458,472,474],{"href":473},"\u002Fdocs\u002Ftechniques\u002Frate-limiting","Rate limiting",[58,476,30],{"id":29},[441,478,479,490],{},[444,480,481,482,485,486,489],{},"Guards that need dependencies use ",[49,483,484],{},"#[injectable]"," + ",[49,487,488],{},"#[inject]"," like any provider.",[444,491,492,493,496,497,500,501,504],{},"Guard metadata (",[49,494,495],{},"guard_types",") is recorded per-route and consumed by ",[49,498,499],{},"ferrite-swagger"," to add ",[49,502,503],{},"401"," + security schemes to generated specs.",[58,506,33],{"id":32},[441,508,509,515],{},[444,510,511,200],{},[458,512,514],{"href":513},"\u002Fdocs\u002Foverview\u002Finterceptors","Interceptors",[444,516,517,200],{},[458,518,520],{"href":519},"\u002Fdocs\u002Foverview\u002Fexception-filters","Exception filters",{"title":12,"searchDepth":13,"depth":14,"links":522},[523,524,525,526,527,528],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21},{"id":23,"depth":13,"text":24},{"id":26,"depth":13,"text":27},{"id":29,"depth":13,"text":30},{"id":32,"depth":13,"text":33},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Foverview\u002Fguards.md",{"title":531,"slug":532},"Middleware","overview\u002Fmiddleware",{"title":514,"slug":534},"overview\u002Finterceptors",1789565440273]