Spring Security auto-configuration helps you add security to your app quickly without writing lots of setup code.
Spring Security auto-configuration in Spring Boot
Start learning this pattern below
Jump into concepts and practice - no test required
spring-boot-starter-security dependency in build file @Configuration @EnableWebSecurity public class SecurityConfig { // Optional customizations }
Just adding the spring-boot-starter-security dependency triggers auto-configuration.
You can override defaults by creating your own @Configuration class.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
}@SpringBootApplication public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } }
@Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.authorizeHttpRequests(authz -> authz.anyRequest().authenticated()) .formLogin(); return http.build(); } }
This simple Spring Boot app uses Spring Security auto-configuration. When you run it and visit http://localhost:8080, it will ask for a login because security is enabled by default.
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
By default, Spring Security creates a user with a generated password printed in the console.
You can customize users and passwords in application.properties or with your own config class.
Auto-configuration saves time but you can always override it for full control.
Adding spring-boot-starter-security enables default security automatically.
It protects all web endpoints with a login page by default.
You can customize or disable auto-configuration by providing your own security setup.
Practice
spring-boot-starter-security to a Spring Boot project without any additional configuration?Solution
Step 1: Understand default behavior of spring-boot-starter-security
Adding this starter enables Spring Security auto-configuration which secures all web endpoints by default.Step 2: Recognize the default login page
Spring Security provides a default login page automatically when no custom security config is provided.Final Answer:
All web endpoints are secured with a default login page. -> Option BQuick Check:
Default security = secured endpoints + login page [OK]
- Thinking endpoints remain open without config
- Assuming only REST endpoints are secured
- Believing an error occurs without config
Solution
Step 1: Identify the correct annotation to exclude auto-configuration
Spring Boot allows excluding auto-configurations via theexcludeattribute in@SpringBootApplication.Step 2: Confirm the correct class to exclude
The class to exclude for disabling security auto-configuration isSecurityAutoConfiguration.class.Final Answer:
@SpringBootApplication(exclude = SecurityAutoConfiguration.class) -> Option DQuick Check:
Disable auto-config = exclude in @SpringBootApplication [OK]
- Using @EnableAutoConfiguration instead of @SpringBootApplication
- Trying to exclude in @ComponentScan or @Configuration
- Not specifying the correct class to exclude
spring-boot-starter-security added and no custom security config, what will happen when a user accesses /hello endpoint? @RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello World";
}
}Solution
Step 1: Recall default security behavior with no custom config
All endpoints are secured and require authentication by default.Step 2: Understand access flow to /hello endpoint
Accessing /hello triggers Spring Security to redirect to the default login page before allowing access.Final Answer:
The user is redirected to a login page before seeing "Hello World". -> Option CQuick Check:
Default security = login redirect before access [OK]
- Assuming endpoints are open without login
- Expecting 404 error for existing endpoint
- Thinking runtime exception occurs
spring-boot-starter-security but your application fails to start with a bean creation error related to AuthenticationManager. What is the likely cause?Solution
Step 1: Understand the error context with AuthenticationManager bean
When customizing security by defining aSecurityFilterChain, Spring Boot no longer auto-configuresAuthenticationManager.Step 2: Identify missing bean definition
You must manually expose anAuthenticationManagerbean to satisfy dependencies.Final Answer:
You defined a custom SecurityFilterChain but forgot to expose an AuthenticationManager bean. -> Option AQuick Check:
Custom filter chain needs AuthenticationManager bean [OK]
- Blaming missing web dependency
- Ignoring need for AuthenticationManager bean
- Assuming multiple @SpringBootApplication causes this error
/public/** endpoints but secure all others with form login. Which configuration snippet correctly achieves this?Solution
Step 1: Use SecurityFilterChain bean to customize security rules
Spring Security 5.7+ recommends defining aSecurityFilterChainbean for custom rules.Step 2: Permit /public/** and require authentication for others with form login
The method chainauthorizeHttpRequests().requestMatchers("/public/**").permitAll().anyRequest().authenticated().and().formLogin()correctly sets these rules.Final Answer:
Define a SecurityFilterChain bean with http.authorizeHttpRequests().requestMatchers("/public/**").permitAll().anyRequest().authenticated().and().formLogin() -> Option AQuick Check:
Permit public paths + secure others + form login = Define aSecurityFilterChainbean withhttp.authorizeHttpRequests().requestMatchers("/public/**").permitAll().anyRequest().authenticated().and().formLogin(). [OK]
- Excluding auto-config and using deprecated WebSecurityConfigurerAdapter
- Disabling form login when it is required
- Misusing @EnableWebSecurity without proper bean
