Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 38

    Spring Security Introduction

    Spring Security is the standard authentication and authorization stack for Spring Boot APIs.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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 STATELESS sessions + bearer tokens.

    Step-by-step explanation

    1. Add spring-boot-starter-security (and later oauth2-resource-server when using JWT).
    2. Create SecurityConfig with @Configuration and @EnableWebSecurity.
    3. Declare a @Bean SecurityFilterChain that permits auth/health and authenticates Orders routes.
    4. Start the app; confirm unauthenticated GET /api/orders/1 is rejected.

    Informative example

    Minimal Security 6 config for a stateless Orders API.

    java
    @Configuration
    @EnableWebSecurity
    public class SecurityConfig {
    @Bean
    SecurityFilterChain 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 SecurityFilterChain per concern (API vs actuator) when rules diverge.
    • Prefer explicit requestMatchers allow-lists over broad permitAll.
    • Treat security config as production code — review diffs like business logic.

    Common mistakes

    • Copying WebSecurityConfigurerAdapter samples — 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.

    3 questions
    1BeginnerQuestionWhat replaced WebSecurityConfigurerAdapter?+

    Answer

    Component-based config: declare a SecurityFilterChain @Bean and configure HttpSecurity with the lambda DSL.
    2BeginnerQuestionWhy is Spring Security secure by default?+

    Answer

    With the starter on the classpath, requests require authentication unless you explicitly permit matchers — reducing accidental open APIs.
    3IntermediateQuestionWhen is STATELESS session policy appropriate?+

    Answer

    For APIs that authenticate each request via bearer tokens (JWT) and do not rely on server HTTP sessions.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.