Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
SecurityFilterChain configuration
📖 Scenario: You are building a simple Spring Boot web application that needs basic security setup.You want to configure which URLs require authentication and which do not.
🎯 Goal: Create a SecurityFilterChain bean that configures HTTP security to allow public access to the home page and require authentication for all other requests.
📋 What You'll Learn
Create a SecurityFilterChain bean method named securityFilterChain
Use HttpSecurity to configure security
Allow all requests to / without authentication
Require authentication for any other request
Enable form login for authentication
💡 Why This Matters
🌍 Real World
Web applications often need to control which pages users can see without logging in and which require login. This setup is a common starting point.
💼 Career
Understanding SecurityFilterChain configuration is essential for backend developers working with Spring Boot to secure web applications.
Progress0 / 4 steps
1
Create SecurityFilterChain bean method
Create a method named securityFilterChain that returns SecurityFilterChain and accepts a parameter HttpSecurity http. Annotate this method with @Bean.
Spring Boot
Hint
Define a method with @Bean annotation that returns SecurityFilterChain and takes HttpSecurity http as parameter.
2
Configure public access to home page
Inside the securityFilterChain method, configure http to allow all requests to "/" without authentication using authorizeHttpRequests and requestMatchers.
Spring Boot
Hint
Use http.authorizeHttpRequests(auth -> auth.requestMatchers("/").permitAll()) to allow public access to the home page.
3
Require authentication for other requests and enable form login
Extend the http configuration to require authentication for any request other than "/" by adding anyRequest().authenticated(). Also enable form login by calling formLogin().
Spring Boot
Hint
Chain .anyRequest().authenticated() and .formLogin() after permitting "/".
4
Return the built SecurityFilterChain
Complete the securityFilterChain method by returning the built SecurityFilterChain from http.build().
Spring Boot
Hint
Use return http.build(); to return the configured SecurityFilterChain.
Practice
(1/5)
1. What is the primary purpose of a SecurityFilterChain in Spring Boot?
easy
A. To handle file uploads
B. To define security rules for web requests and control access
C. To manage application logging levels
D. To configure database connections
Solution
Step 1: Understand the role of SecurityFilterChain
The SecurityFilterChain is used in Spring Security to define how HTTP requests are secured, including which URLs require authentication and what roles can access them.
Step 2: Compare with other options
Database connections, logging, and file uploads are unrelated to SecurityFilterChain's purpose.
Final Answer:
To define security rules for web requests and control access -> Option B
Quick Check:
SecurityFilterChain controls web security = D [OK]
Hint: SecurityFilterChain controls web request security rules [OK]
Common Mistakes:
Confusing SecurityFilterChain with database or logging config
Thinking it manages file uploads
Assuming it handles application-wide settings
2. Which of the following is the correct way to declare a SecurityFilterChain bean in Spring Boot?
easy
A. @Component public void filterChain(HttpSecurity http) { http.build(); }
B. public SecurityFilterChain filterChain() { return new SecurityFilterChain(); }
C. @Bean public void filterChain() { return http.build(); }
D. @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { return http.build(); }
Solution
Step 1: Identify correct bean declaration syntax
In Spring Boot, a SecurityFilterChain bean must be annotated with @Bean, accept HttpSecurity as a parameter, and return the built chain with http.build().
Step 2: Check each option
@Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { return http.build(); } correctly uses @Bean, returns SecurityFilterChain, and calls http.build(). Options B and D have wrong return types or missing annotations. @Component public void filterChain(HttpSecurity http) { http.build(); } uses @Component and void return, which is incorrect.
Final Answer:
@Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { return http.build(); } -> Option D
Quick Check:
Correct bean method signature = C [OK]
Hint: Bean method must return SecurityFilterChain and use @Bean [OK]
Common Mistakes:
Forgetting @Bean annotation
Using void return type
Not passing HttpSecurity parameter
3. Given this SecurityFilterChain configuration snippet, what will happen when a user accesses /admin URL?
D. Using permitAll() before authenticated() causes error
Solution
Step 1: Check method signature for exceptions
The http.build() method can throw a checked exception, so the method should declare throws Exception.
Step 2: Verify return statement and method correctness
The method returns http.build() correctly. The order of authenticated() and permitAll() is valid. So the only issue is missing exception declaration.
Final Answer:
Missing throws Exception in method signature -> Option A
Quick Check:
http.build() may throw Exception = B [OK]
Hint: Add throws Exception when calling http.build() [OK]
Common Mistakes:
Omitting throws Exception causes compile error
Misunderstanding order of permitAll and authenticated
Forgetting to return http.build()
5. You want to configure a SecurityFilterChain that allows anonymous access to /public/**, requires authentication for /user/**, and restricts /admin/** to users with role ADMIN, and denies access to all other requests. Which configuration snippet correctly implements this?
hard
A. @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**").permitAll()
.requestMatchers("/user/**").authenticated()
.requestMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().denyAll()
).formLogin();
return http.build();
}
B. @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/admin/**").hasRole("ADMIN")
.requestMatchers("/user/**").authenticated()
.requestMatchers("/public/**").permitAll()
.anyRequest().authenticated()
).formLogin();
return http.build();
}
C. @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**").authenticated()
.requestMatchers("/user/**").permitAll()
.requestMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().denyAll()
).formLogin();
return http.build();
}
D. @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**").permitAll()
.requestMatchers("/user/**").permitAll()
.requestMatchers("/admin/**").authenticated()
.anyRequest().denyAll()
).formLogin();
return http.build();
}
Solution
Step 1: Match access rules to URL patterns
The requirement is: /public/** open to all (permitAll), /user/** requires authentication, /admin/** requires ADMIN role, and all others denied.
Step 2: Check each option's order and rules
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**").permitAll()
.requestMatchers("/user/**").authenticated()
.requestMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().denyAll()
).formLogin();
return http.build();
} matches the requirements exactly. @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/admin/**").hasRole("ADMIN")
.requestMatchers("/user/**").authenticated()
.requestMatchers("/public/**").permitAll()
.anyRequest().authenticated()
).formLogin();
return http.build();
} allows anyRequest authenticated (not denyAll). @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**").authenticated()
.requestMatchers("/user/**").permitAll()
.requestMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().denyAll()
).formLogin();
return http.build();
} swaps permitAll and authenticated for /public and /user incorrectly. @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**").permitAll()
.requestMatchers("/user/**").permitAll()
.requestMatchers("/admin/**").authenticated()
.anyRequest().denyAll()
).formLogin();
return http.build();
} permits /user/** to all and only authenticates /admin/**, which is wrong.