Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Spring
Springhard

Spring Security — where do authentication/authorization happen?

Tags
#spring-security#filters#auth
Back to categoryPractice quiz

Answer

Mainly in the security filter chain, before the request reaches controllers. Filters build the `SecurityContext` (authentication), then authorization checks decide if access is allowed (URL rules, method security, etc.).

Advanced answer

Deep dive

Spring Security sits in front of your MVC layer as a chain of servlet filters (Security Filter Chain). Every request flows through filters before hitting controllers.

Authentication

  • A filter extracts credentials (session, basic auth, JWT bearer token, etc.).
  • `AuthenticationManager` delegates to one or more `AuthenticationProvider`s.
  • On success, an `Authentication` is stored in `SecurityContextHolder`.

Authorization

Authorization decisions are made for each request (and sometimes per method call) based on:

  • URL rules (`authorizeHttpRequests`), and/or
  • method security (`@PreAuthorize`, `@PostAuthorize`).

Example

@Bean
SecurityFilterChain security(HttpSecurity http) throws Exception {
  return http
    .authorizeHttpRequests(a -> a
      .requestMatchers("/admin/**").hasRole("ADMIN")
      .anyRequest().authenticated())
    .build();
}

@PreAuthorize("hasRole('ADMIN')")
void deleteUser(long id) { /* ... */ }

Common pitfalls

  • Assuming authorization is checked only once at login (it’s evaluated per request / per method).
  • Misunderstanding CSRF defaults in browser-based apps.

Related questions

Spring
Spring Security context — why can auth break in async code?
#spring-security#securitycontext#threadlocal
Security
JWT pitfalls: when should you use JWTs vs server-side sessions?
#jwt#sessions#auth
Next.js
Next.js App Router auth: how do you keep it secure and SSR-friendly?
Forgetting to enable/configure method security depending on the Spring Security version.
#nextjs#auth#cookies
Architecture
Authentication vs authorization — what’s the difference?
#auth#authentication#authorization