liveBook · Manning

SecurityFilterChain

As explained in Spring Security in Action, Second Edition

SecurityFilterChain is a Spring Security component that processes HTTP requests through security filters and applies web security rules.

SecurityFilterChain defines how Spring Security handles HTTP authentication and endpoint authorization. Define it as a bean when the defaults do not match the application. section 2.3.2

Configure the chain through HttpSecurity:

  • Call httpBasic(Customizer.withDefaults()) to enable HTTP Basic authentication. section 2.3.2
  • Call authorizeHttpRequests() to define endpoint authorization, such as anyRequest().authenticated() or anyRequest().permitAll(). section 2.3.2
  • Register a UserDetailsService locally with userDetailsService(), or provide it separately as a Spring bean. section 2.3.3

Protect every endpoint

A minimal configuration enables HTTP Basic authentication and requires authentication for every request. Replace authenticated() with permitAll() when endpoints should accept requests without credentials.

Listing 2.9 Setting UserDetailsService with the SecurityFilterChain bean
@Configuration
public class ProjectConfig {

  @Bean
  public SecurityFilterChain configure(HttpSecurity http) 
    throws Exception {

    http.httpBasic(Customizer.withDefaults());
    http.authorizeHttpRequests(
        c -> c.anyRequest().authenticated()
    );

    var user = User.withUsername("john")              #1
        .password("12345")
        .authorities("read")
        .build();

    var userDetailsService =                          #2
      new InMemoryUserDetailsManager(user);

    http.userDetailsService(userDetailsService);      #3

    return http.build();  
}

  // Omitted code

}

Inspect a complete chain that creates an in-memory user, registers its UserDetailsService, and returns the built configuration. section 2.3.3

How Spring Security in Action, Second Edition covers it

How deep it goes
Explains bean construction, HttpSecurity methods, service registration, class separation, and filter ordering customization.
Examples
Uses authenticated or permitAll request rules, an in-memory user, and separate user-management and authorization configuration classes.
What it assumes
Assumes familiarity with Spring beans, Java configuration, HTTP requests, authentication, and authorization terminology.
What you can do after
Build it from scratch by defining a SecurityFilterChain bean and configuring HttpSecurity with authentication and authorization rules.
How it connects
Connects HTTP Basic authentication, endpoint authorization, HttpSecurity, UserDetailsService, PasswordEncoder, and servlet filters.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Security in Action, Second Edition. The section links point to the parts of the book it draws on.