NestJS Guards for Spring Developers: The Route You Forgot

30 September 2026 nestjstypescriptnodejsjavaspring-bootsecurity
PDF The design sheet The full architecture and the verified references. Free, no email.
Thumbnail for NestJS Guards for Spring Developers: The Route You Forgot 4:10
Watch on YouTube, or read on below.
Run this yourself → Clone the repo, 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.


Watch next

New videos land on Code with Sam - subscribe there if this was useful.

Get the interview pack

A free, practical pack for engineers preparing for senior backend interviews - the questions that actually get asked, and how to answer them like someone who has done the work.

New articles and videos when they land. No spam, and one click to leave.