SecurityFilterChain in Spring Security Explained with Real Examples

SecurityFilterChain is one of the most important concepts in modern Spring Security. Since Spring Security 5.7, SecurityFilterChain has become the recommended approach for defining security configurations.


Understanding SecurityFilterChain is essential for securing Spring Boot applications and frequently appears in Spring Security interviews.


What is SecurityFilterChain?

SecurityFilterChain is a sequence of security filters that process every incoming HTTP request before it reaches a controller.

Each filter performs a specific security task such as authentication, authorization or JWT validation.



Request
   |
   v
Security Filters
   |
   v
Controller

Why Do We Need SecurityFilterChain?

Without SecurityFilterChain, requests would directly reach application endpoints without security validation.

SecurityFilterChain acts as a security checkpoint.



Client
   |
   v
SecurityFilterChain
   |
   v
Protected API

Request Processing Flow


Client Request
       |
       v
Authentication Filter
       |
       v
Authorization Filter
       |
       v
Security Context
       |
       v
Controller

Every request must successfully pass through the chain.


Creating a SecurityFilterChain


@Bean

public SecurityFilterChain
securityFilterChain(
        HttpSecurity http)
throws Exception {

    http

        .authorizeHttpRequests(

            auth -> auth

                .anyRequest()
                .authenticated()

        )

        .formLogin();

    return http.build();

}

This configuration requires authentication for every endpoint.


Understanding HttpSecurity

HttpSecurity is used to configure web security rules.


HttpSecurity

It controls authentication, authorization, CORS, CSRF and session management.


Allowing Public Endpoints


.authorizeHttpRequests(

    auth -> auth

    .requestMatchers(
        "/public/**"
    )

    .permitAll()

)

Anyone can access these endpoints without authentication.


Securing Specific Endpoints


.authorizeHttpRequests(

    auth -> auth

    .requestMatchers(
        "/admin/**"
    )

    .hasRole("ADMIN")

)

Only ADMIN users can access those endpoints.


Multiple Security Rules


.authorizeHttpRequests(

    auth -> auth

        .requestMatchers(
            "/public/**"
        )

        .permitAll()

        .requestMatchers(
            "/admin/**"
        )

        .hasRole("ADMIN")

        .requestMatchers(
            "/manager/**"
        )

        .hasRole("MANAGER")

        .anyRequest()

        .authenticated()

)

Security Context

Spring Security stores authenticated user information inside SecurityContext.


SecurityContextHolder

Controllers and services can access the current authenticated user.


Accessing Current User


Authentication auth =

SecurityContextHolder
    .getContext()
    .getAuthentication();

This returns details about the currently logged-in user.


JWT and SecurityFilterChain

JWT authentication is implemented by adding a custom JWT filter into SecurityFilterChain.


JWT Token
     |
     v
JWT Filter
     |
     v
Security Context

This enables stateless authentication.


Disabling CSRF

REST APIs often disable CSRF protection.



http.csrf(
    csrf -> csrf.disable()
);

This is common in JWT-based applications.


Common Filters in Spring Security

  • UsernamePasswordAuthenticationFilter
  • BasicAuthenticationFilter
  • JwtAuthenticationFilter
  • LogoutFilter
  • ExceptionTranslationFilter

SecurityFilterChain vs WebSecurityConfigurerAdapter

SecurityFilterChain WebSecurityConfigurerAdapter
Modern Approach Deprecated
Bean Based Inheritance Based
Spring Security 5.7+ Older Versions

Real World Example

An e-commerce platform may secure resources as follows:


/public/products

/admin/products

/admin/orders

/customer/orders

SecurityFilterChain determines who can access each endpoint.


Common Interview Questions

What is SecurityFilterChain?

It is a chain of security filters that process incoming requests before controllers are invoked.


Why was WebSecurityConfigurerAdapter removed?

Spring Security moved to a bean-based configuration model using SecurityFilterChain.


Can SecurityFilterChain work with JWT?

Yes. JWT validation is commonly implemented through custom filters.


What is HttpSecurity?

A configuration object used to define security rules.


What is SecurityContext?

A container that stores information about the authenticated user.


Best Practices

  • Use SecurityFilterChain instead of deprecated approaches.
  • Keep security rules simple and clear.
  • Use role-based authorization.
  • Protect sensitive endpoints.
  • Combine with JWT for REST APIs.

Frequently Asked Questions (FAQ)

Can multiple SecurityFilterChain beans exist?

Yes. Applications can define multiple security configurations.


Can SecurityFilterChain secure REST APIs?

Yes. It is commonly used in modern Spring Boot API security.


Should SecurityFilterChain be used with JWT?

Yes. This is a common modern security architecture.


Conclusion

SecurityFilterChain is the foundation of modern Spring Security. It processes incoming requests, performs authentication and authorization checks, and protects application resources. Understanding SecurityFilterChain is essential for building secure Spring Boot applications and succeeding in Spring Security interviews.


Keywords

SecurityFilterChain, Spring Security Filter Chain, Spring Boot Security, Spring Security Configuration, HttpSecurity, Spring Security Tutorial, JWT SecurityFilterChain, Spring Security Interview Questions, Authorization Filter, Authentication Filter, Spring Boot Authentication, Spring Boot Authorization, Java Security, Enterprise Application Security, Spring Security Architecture

SecurityFilterChain in Spring Security Explained with Real Examples SecurityFilterChain in Spring Security Explained with Real Examples Reviewed by 13thGhost on 9:57 PM Rating: 5

Recommended Posts

Powered by Blogger.