Authentication flow helps check who a user is before letting them use an app. It keeps the app safe by making sure only real users get access.
Authentication flow in Spring Boot
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Spring Boot
public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } }
This is a basic Spring Security setup for authentication flow.
It defines which URLs need login and which donโt.
Examples
Spring Boot
http.authorizeRequests()
.antMatchers("/home", "/about").permitAll()
.anyRequest().authenticated();Spring Boot
http.formLogin()
.loginPage("/custom-login")
.defaultSuccessUrl("/dashboard", true);Spring Boot
http.logout()
.logoutUrl("/signout")
.logoutSuccessUrl("/goodbye");Sample Program
This Spring Boot app sets up a simple authentication flow. Public URLs under /public are open to all. Other URLs require login. It uses a form login page at /login and allows logout.
Spring Boot
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.web.SecurityFilterChain; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @EnableWebSecurity public static class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(auth -> auth .requestMatchers("/public/**").permitAll() .anyRequest().authenticated() ) .formLogin(form -> form .loginPage("/login") .permitAll() ) .logout(logout -> logout .permitAll() ); return http.build(); } } }
Important Notes
Spring Security uses filters to check authentication on each request.
Always protect sensitive URLs by requiring authentication.
Customize login and logout pages for better user experience.
Summary
Authentication flow checks user identity before access.
Spring Security configures which pages need login.
Use form login and logout to manage user sessions.
Practice
1. What is the main purpose of the authentication flow in a Spring Boot application?
easy
Solution
Step 1: Understand authentication flow purpose
Authentication flow is about checking who the user is before allowing access.Step 2: Identify correct purpose in options
Only To verify the identity of a user before granting access describes verifying user identity, which matches authentication.Final Answer:
To verify the identity of a user before granting access -> Option DQuick Check:
Authentication = Verify user identity [OK]
Hint: Authentication means checking who the user is [OK]
Common Mistakes:
- Confusing authentication with styling or data storage
- Thinking authentication sends emails
- Mixing authentication with authorization
2. Which of the following is the correct way to configure URL access rules in Spring Security?
easy
Solution
Step 1: Identify correct method for URL rules in Spring Security
Spring Security 6+ uses http.authorizeHttpRequests() with requestMatchers() for URL patterns.Step 2: Check which option uses correct syntax and meaning
http.authorizeHttpRequests().requestMatchers("/admin/**").authenticated() uses authorizeHttpRequests() and requestMatchers() with authenticated(), which is correct.Final Answer:
http.authorizeHttpRequests().requestMatchers("/admin/**").authenticated() -> Option AQuick Check:
Use authorizeHttpRequests() + requestMatchers() [OK]
Hint: Use authorizeHttpRequests() with requestMatchers() in Spring Security 6+ [OK]
Common Mistakes:
- Using deprecated authorizeRequests() in new Spring versions
- Using denyAll() incorrectly for access control
- Using anyRequest().allow() which is invalid
3. Given this Spring Security configuration snippet, what happens when a user accesses
/dashboard without logging in?http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/public/**").permitAll()
.anyRequest().authenticated()
)
.formLogin();medium
Solution
Step 1: Analyze URL access rules
/public/** URLs are open, but any other request requires authentication.Step 2: Check behavior for unauthenticated access to /dashboard
Since /dashboard is not under /public, it requires login. formLogin() triggers redirect to login page.Final Answer:
The user is redirected to the login page -> Option BQuick Check:
Unauthenticated access redirects to login [OK]
Hint: AnyRequest().authenticated() means login required [OK]
Common Mistakes:
- Thinking permitAll() applies to all URLs
- Expecting 403 error instead of redirect
- Assuming access without login
4. Identify the error in this Spring Security configuration snippet:
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/admin/**").permitAll()
.anyRequest().authenticated()
)
.formLogin();medium
Solution
Step 1: Review access rules for /admin/**
permitAll() means anyone can access /admin/** without login, which is usually a security risk.Step 2: Check order and methods
Order is correct; anyRequest().authenticated() applies after permitAll(). formLogin() without URL uses default login page, which is valid.Final Answer:
permitAll() on /admin/** allows unrestricted access to admin pages -> Option CQuick Check:
permitAll() means open access [OK]
Hint: permitAll() means no login needed, risky on admin URLs [OK]
Common Mistakes:
- Thinking order of matchers is wrong here
- Assuming formLogin() needs explicit URL
- Confusing requestMatchers() with antMatchers()
5. You want to create a custom authentication flow that checks a user's email and password against a database and then grants access. Which Spring Boot component should you implement to handle this logic?
hard
Solution
Step 1: Identify component for loading user info
UserDetailsService is designed to load user details like email and password from a database.Step 2: Identify component for password checking
PasswordEncoder is used to verify the password matches the stored hash securely.Step 3: Confirm other options are unrelated
AuthenticationEntryPoint handles unauthorized access, not authentication logic. CorsConfiguration and HttpFirewall serve different purposes.Final Answer:
UserDetailsService to load user data and PasswordEncoder to check password -> Option AQuick Check:
Custom auth uses UserDetailsService + PasswordEncoder [OK]
Hint: UserDetailsService loads users; PasswordEncoder checks passwords [OK]
Common Mistakes:
- Confusing AuthenticationEntryPoint with authentication logic
- Using CorsConfiguration for authentication
- Thinking HttpFirewall handles login checks
