Bird
Raised Fist0
Spring Bootframework~10 mins

Authentication flow in Spring Boot - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Spring Boot controller method that handles login requests.

Spring Boot
public ResponseEntity<String> login(@RequestBody LoginRequest [1]) {
    // method body
}
Drag options to blanks, or click blank then click option'
ArequestBody
BloginRequest
Cuser
Dcredentials
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic names like 'user' or 'credentials' that are less clear.
Using names that conflict with Spring annotations.
2fill in blank
medium

Complete the code to inject the authentication manager bean into the service class.

Spring Boot
@Autowired
private [1] authenticationManager;
Drag options to blanks, or click blank then click option'
AAuthenticationService
BSecurityManager
CAuthManager
DAuthenticationManager
Attempts:
3 left
💡 Hint
Common Mistakes
Using custom or incorrect class names.
Confusing with service or manager classes not related to authentication.
3fill in blank
hard

Fix the error in the code that authenticates a user with username and password.

Spring Boot
Authentication authentication = authenticationManager.authenticate(
    new UsernamePasswordAuthenticationToken([1], password)
);
Drag options to blanks, or click blank then click option'
Apassword
Bauthentication
Cusername
DuserDetails
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping username and password parameters.
Passing the whole authentication object instead of username.
4fill in blank
hard

Fill both blanks to create a JWT token after successful authentication.

Spring Boot
String token = jwtUtil.[1](authentication.getName(), [2]);
Drag options to blanks, or click blank then click option'
AgenerateToken
BvalidateToken
Cauthentication.getAuthorities()
Dauthentication.getCredentials()
Attempts:
3 left
💡 Hint
Common Mistakes
Using token validation method instead of generation.
Passing credentials instead of authorities.
5fill in blank
hard

Fill all three blanks to configure HTTP security to require authentication for all requests except login.

Spring Boot
http.csrf().disable()
    .authorizeHttpRequests()
    .requestMatchers("/login").[1]()
    .anyRequest().[2]()
    .and()
    .sessionManagement().sessionCreationPolicy([3]);
Drag options to blanks, or click blank then click option'
ApermitAll
Bauthenticated
CSessionCreationPolicy.STATELESS
DSessionCreationPolicy.ALWAYS
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to disable CSRF for stateless APIs.
Using wrong session creation policy causing stateful sessions.
Not permitting access to login endpoint.

Practice

(1/5)
1. What is the main purpose of the authentication flow in a Spring Boot application?
easy
A. To send emails to users after login
B. To style the user interface of the login page
C. To store user data in the database
D. To verify the identity of a user before granting access

Solution

  1. Step 1: Understand authentication flow purpose

    Authentication flow is about checking who the user is before allowing access.
  2. 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.
  3. Final Answer:

    To verify the identity of a user before granting access -> Option D
  4. Quick 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
A. http.authorizeHttpRequests().requestMatchers("/admin/**").authenticated()
B. http.authorizeRequests().antMatchers("/private/**").denyAll()
C. http.authorizeRequests().anyRequest().allow()
D. http.authorizeRequests().requestMatchers("/public/**").permitAll()

Solution

  1. Step 1: Identify correct method for URL rules in Spring Security

    Spring Security 6+ uses http.authorizeHttpRequests() with requestMatchers() for URL patterns.
  2. Step 2: Check which option uses correct syntax and meaning

    http.authorizeHttpRequests().requestMatchers("/admin/**").authenticated() uses authorizeHttpRequests() and requestMatchers() with authenticated(), which is correct.
  3. Final Answer:

    http.authorizeHttpRequests().requestMatchers("/admin/**").authenticated() -> Option A
  4. Quick 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
A. The user can access /dashboard without login
B. The user is redirected to the login page
C. The user gets a 403 Forbidden error
D. The user sees a blank page

Solution

  1. Step 1: Analyze URL access rules

    /public/** URLs are open, but any other request requires authentication.
  2. Step 2: Check behavior for unauthenticated access to /dashboard

    Since /dashboard is not under /public, it requires login. formLogin() triggers redirect to login page.
  3. Final Answer:

    The user is redirected to the login page -> Option B
  4. Quick 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
A. formLogin() is missing a login page URL
B. anyRequest().authenticated() should come before requestMatchers()
C. permitAll() on /admin/** allows unrestricted access to admin pages
D. requestMatchers() should be replaced with antMatchers()

Solution

  1. Step 1: Review access rules for /admin/**

    permitAll() means anyone can access /admin/** without login, which is usually a security risk.
  2. Step 2: Check order and methods

    Order is correct; anyRequest().authenticated() applies after permitAll(). formLogin() without URL uses default login page, which is valid.
  3. Final Answer:

    permitAll() on /admin/** allows unrestricted access to admin pages -> Option C
  4. Quick 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
A. UserDetailsService to load user data and PasswordEncoder to check password
B. AuthenticationEntryPoint to redirect users after login
C. CorsConfiguration to allow cross-origin requests
D. HttpFirewall to block unauthorized IP addresses

Solution

  1. Step 1: Identify component for loading user info

    UserDetailsService is designed to load user details like email and password from a database.
  2. Step 2: Identify component for password checking

    PasswordEncoder is used to verify the password matches the stored hash securely.
  3. Step 3: Confirm other options are unrelated

    AuthenticationEntryPoint handles unauthorized access, not authentication logic. CorsConfiguration and HttpFirewall serve different purposes.
  4. Final Answer:

    UserDetailsService to load user data and PasswordEncoder to check password -> Option A
  5. Quick 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