NestJS Guards for Spring Developers: The Route You Forgot
The design sheet The full architecture and the verified references. Free, no email.
4:10 docker compose up, and watch it happen
A payments API with a guard on the payments controller. Someone adds a refunds endpoint and writes no security code for it. An anonymous request to the new route answered 401 in Spring Boot and 200 in NestJS. Measured on 30 September 2026.
The difference is scope
With Spring Security on the classpath, Spring Boot secures web applications by default: the filter chain sits in front of every route. A Nest guard protects exactly what it is bound to, a handler, a controller, or the whole application if you register it that way. The refunds controller was never bound, so no guard ran.
Make it global
Register the guard as APP_GUARD and mark the routes that really are open with a @Public()
decorator the guard reads through the Reflector. Payments 401, refunds 401, health 200. Now
forgetting protects the route instead of exposing it.
401 or 403
A guard that returns false gives 403. A missing or invalid token is unauthenticated, which is a 401, so
throw UnauthorizedException for it and keep 403 for a caller who is known but not allowed.
Method authorization
@PreAuthorize("hasRole('ADMIN')") did nothing until @EnableMethodSecurity was on: a plain user
got 200 and a direct call ran. With it, 403, and the direct call was refused. A Nest @Roles guard
refused a user through the route, but the service called directly still ran. This guard protects
entry through the route, not the method.
The full code for every measurement is in the repository, and the free design sheet above has the checklist on one page.