Spring Security Introduction
Spring Security is the standard authentication and authorization stack for Spring Boot APIs.
Introduction
Spring Security is the standard authentication and authorization stack for Spring Boot APIs. Adding spring-boot-starter-security secures endpoints by default — you opt out explicitly for health and login routes.
On Spring Security 6 (Boot 3), configuration is a SecurityFilterChain @Bean. WebSecurityConfigurerAdapter is gone. You build the chain with the HttpSecurity DSL: CSRF policy, session policy, authorize rules, and resource-server/JWT hooks.
Every request walks a filter chain. Successful authentication populates the SecurityContext; authorization rules decide whether the principal may reach the Orders controller.
This lesson is the map. Later lessons deepen authentication, authorization, JWT, filters, and OAuth2 — always against the TechLearningPRO Orders API.
Purpose of this lesson
Explain Spring Security 6's filter-based model and write a minimal Orders SecurityFilterChain without WebSecurityConfigurerAdapter.
Understanding the topic
Security is a chain of filters plus a context holding the principal.
- SecurityFilterChain: ordered filters wrapping the servlet pipeline.
- Authentication: who is calling (password, JWT bearer, OAuth2).
- Authorization: what that principal may do (URL and method rules).
- SecurityContext: per-request holder for
Authentication. - Stateless APIs: typically
STATELESSsessions + bearer tokens.
Step-by-step explanation
- Add
spring-boot-starter-security(and later oauth2-resource-server when using JWT). - Create
SecurityConfigwith@Configurationand@EnableWebSecurity. - Declare a
@Bean SecurityFilterChainthat permits auth/health and authenticates Orders routes. - Start the app; confirm unauthenticated
GET /api/orders/1is rejected.
Informative example
Minimal Security 6 config for a stateless Orders API.
@Configuration@EnableWebSecuritypublic class SecurityConfig {@BeanSecurityFilterChain api(HttpSecurity http) throws Exception {return http.csrf(csrf -> csrf.disable()).sessionManagement(s -> s.sessionCreationPolicy(SessionCreationPolicy.STATELESS)).authorizeHttpRequests(auth -> auth.requestMatchers("/api/auth/**", "/actuator/health").permitAll().requestMatchers("/api/admin/**").hasRole("ADMIN").anyRequest().authenticated()).httpBasic(Customizer.withDefaults()).build();}}
Real-world use
TechLearningPRO Orders went live once with only network ACLs. A forgotten actuator route leaked environment details. Spring Security's secure-by-default stance forces every path through an explicit permit or authenticate decision — safer than hoping the gateway alone is enough.
Security implications
Misconfigured chains are silent vulnerabilities.
- CSRF disable is for stateless bearer APIs — not for cookie session browser forms without other defenses.
- Actuator endpoints need their own rules; health may be public, env must not be.
- Never commit real client secrets or signing keys beside sample config.
Best practices
- One clear
SecurityFilterChainper concern (API vs actuator) when rules diverge. - Prefer explicit
requestMatchersallow-lists over broadpermitAll. - Treat security config as production code — review diffs like business logic.
Common mistakes
- Copying
WebSecurityConfigurerAdaptersamples — they do not compile on Security 6. - Disabling security entirely in a profile and shipping that profile by mistake.
- Leaving the generated default user password in logs without replacing the user store.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhat replaced WebSecurityConfigurerAdapter?+
Answer
SecurityFilterChain @Bean and configure HttpSecurity with the lambda DSL.2BeginnerQuestionWhy is Spring Security secure by default?+
Answer
3IntermediateQuestionWhen is STATELESS session policy appropriate?+
Answer
Hands-on exercise
Lab. Add the SecurityFilterChain above. Curl /actuator/health (200) and /api/orders/1 without credentials (401/403). Then add a temporary permitAll for orders and observe the change — revert it.
Summary
Spring Security wraps Orders in a filter chain you configure as beans. Next: how authentication proves identity into that chain.