Bird
Raised Fist0
Spring Bootframework~10 mins

Why Spring Security matters in Spring Boot - Test Your Understanding

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 enable basic HTTP security in a Spring Boot application.

Spring Boot
http
  .authorizeHttpRequests()
  .anyRequest().[1]();
Drag options to blanks, or click blank then click option'
Aanonymous
BpermitAll
CdenyAll
Dauthenticated
Attempts:
3 left
💡 Hint
Common Mistakes
Using permitAll() which allows access without login
Using denyAll() which blocks all access
2fill in blank
medium

Complete the code to configure a password encoder bean in Spring Security.

Spring Boot
@Bean
public PasswordEncoder passwordEncoder() {
    return new [1]();
}
Drag options to blanks, or click blank then click option'
AMD5PasswordEncoder
BBCryptPasswordEncoder
CPlainTextPasswordEncoder
DNoOpPasswordEncoder
Attempts:
3 left
💡 Hint
Common Mistakes
Using NoOpPasswordEncoder which stores passwords as plain text
Using MD5PasswordEncoder which is outdated and insecure
3fill in blank
hard

Fix the error in the code to properly configure HTTP Basic authentication.

Spring Boot
http
  .httpBasic()
  .and()
  .authorizeHttpRequests()
  .anyRequest().[1]();
Drag options to blanks, or click blank then click option'
ApermitAll
BdenyAll
Cauthenticated
Danonymous
Attempts:
3 left
💡 Hint
Common Mistakes
Using permitAll() which disables authentication
Using denyAll() which blocks all access
4fill in blank
hard

Fill both blanks to create a security filter chain bean that disables CSRF and requires authentication for all requests.

Spring Boot
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.csrf().[1]();
    http.authorizeHttpRequests().anyRequest().[2]();
    return http.build();
}
Drag options to blanks, or click blank then click option'
Adisable
Bauthenticated
CpermitAll
Denable
Attempts:
3 left
💡 Hint
Common Mistakes
Using enable() instead of disable() for CSRF
Using permitAll() which allows access without login
5fill in blank
hard

Fill all three blanks to configure a custom login page URL, logout URL, and success URL after login.

Spring Boot
http
  .formLogin()
    .loginPage("[1]")
    .defaultSuccessUrl("[3]", true)
  .and()
  .logout()
    .logoutUrl("[2]");
Drag options to blanks, or click blank then click option'
A/custom-login
B/perform-logout
C/home
D/login
Attempts:
3 left
💡 Hint
Common Mistakes
Using default login page URL '/login' instead of custom
Not setting logout URL correctly
Not specifying a success URL after login

Practice

(1/5)
1. Why is Spring Security important in a Spring Boot application?
easy
A. It helps protect the app by controlling who can access what.
B. It automatically improves app performance without configuration.
C. It provides tools for designing user interfaces.
D. It manages database connections efficiently.

Solution

  1. Step 1: Understand the role of Spring Security

    Spring Security is designed to protect applications by managing authentication and authorization.
  2. Step 2: Compare options with Spring Security's purpose

    Only It helps protect the app by controlling who can access what. correctly describes controlling access, which is the core of Spring Security.
  3. Final Answer:

    It helps protect the app by controlling who can access what. -> Option A
  4. Quick Check:

    Security = Access control [OK]
Hint: Spring Security controls access to keep apps safe [OK]
Common Mistakes:
  • Confusing security with performance optimization
  • Thinking it manages UI design
  • Assuming it handles database connections
2. Which of the following is the correct way to enable Spring Security in a Spring Boot project?
easy
A. Add spring-boot-starter-web dependency only.
B. Add the dependency spring-boot-starter-security to your build file.
C. Write a custom security filter without dependencies.
D. Use spring-boot-starter-data-jpa for security.

Solution

  1. Step 1: Identify the dependency for Spring Security

    The official way to add Spring Security is by including spring-boot-starter-security in your project.
  2. Step 2: Eliminate incorrect options

    Options A, C, and D do not enable Spring Security properly; they relate to web, custom code, or database, not security starter.
  3. Final Answer:

    Add the dependency spring-boot-starter-security to your build file. -> Option B
  4. Quick Check:

    Security starter dependency = Add the dependency spring-boot-starter-security to your build file. [OK]
Hint: Add spring-boot-starter-security dependency to enable security [OK]
Common Mistakes:
  • Adding unrelated dependencies
  • Trying to implement security without starter
  • Confusing web or data dependencies with security
3. Given this Spring Security configuration snippet, what will happen when a user tries to access /admin without logging in?
http
  .authorizeHttpRequests(auth -> auth
    .requestMatchers("/admin").authenticated()
    .anyRequest().permitAll()
  )
  .formLogin();
medium
A. The user will see a permission denied message without login.
B. The user can access /admin without logging in.
C. The user will get a 404 error when accessing /admin.
D. The user will be redirected to a login page before accessing /admin.

Solution

  1. Step 1: Analyze the security rules for /admin

    The config requires authentication for /admin and permits all other requests.
  2. Step 2: Understand form login behavior

    Since .formLogin() is enabled, unauthenticated users are redirected to a login page automatically.
  3. Final Answer:

    The user will be redirected to a login page before accessing /admin. -> Option D
  4. Quick Check:

    Authenticated access + formLogin = redirect to login [OK]
Hint: Authenticated paths redirect to login page if not logged in [OK]
Common Mistakes:
  • Assuming access without login
  • Confusing 404 with access denial
  • Thinking permission denied shows without login
4. Identify the error in this Spring Security configuration code:
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  http.authorizeHttpRequests()
    .requestMatchers("/user").authenticated()
    .anyRequest().permitAll();
  return http.build();
}
medium
A. Missing .and() before return statement.
B. The requestMatchers method should be antMatchers.
C. The method authorizeHttpRequests() requires a lambda argument.
D. The http.build() call is incorrect and should be http.buildChain().

Solution

  1. Step 1: Check the usage of authorizeHttpRequests()

    In Spring Security 6+, authorizeHttpRequests() requires a lambda to configure rules.
  2. Step 2: Identify missing lambda argument

    The code calls authorizeHttpRequests() without a lambda, causing a syntax error.
  3. Final Answer:

    The method authorizeHttpRequests() requires a lambda argument. -> Option C
  4. Quick Check:

    authorizeHttpRequests needs lambda = The method authorizeHttpRequests() requires a lambda argument. [OK]
Hint: authorizeHttpRequests needs lambda for rules in Spring Security 6+ [OK]
Common Mistakes:
  • Omitting lambda argument for authorizeHttpRequests
  • Confusing requestMatchers with antMatchers
  • Incorrect method calls on HttpSecurity
5. You want to customize Spring Security to allow only users with role ADMIN to access /admin, but allow everyone else to access /public. Which configuration snippet correctly achieves this?
hard
A.
http
  .authorizeHttpRequests(auth -> auth
    .requestMatchers("/admin").hasRole("ADMIN")
    .requestMatchers("/public").permitAll()
    .anyRequest().denyAll()
  )
  .formLogin();
B.
http
  .authorizeHttpRequests(auth -> auth
    .requestMatchers("/admin").permitAll()
    .requestMatchers("/public").hasRole("ADMIN")
    .anyRequest().authenticated()
  )
  .formLogin();
C.
http
  .authorizeHttpRequests(auth -> auth
    .requestMatchers("/admin").authenticated()
    .requestMatchers("/public").permitAll()
    .anyRequest().hasRole("ADMIN")
  )
  .formLogin();
D.
http
  .authorizeHttpRequests(auth -> auth
    .requestMatchers("/admin").hasAuthority("ADMIN")
    .requestMatchers("/public").permitAll()
    .anyRequest().denyAll()
  )
  .formLogin();

Solution

  1. Step 1: Check role-based access for /admin

    http
      .authorizeHttpRequests(auth -> auth
        .requestMatchers("/admin").hasRole("ADMIN")
        .requestMatchers("/public").permitAll()
        .anyRequest().denyAll()
      )
      .formLogin();
    uses hasRole("ADMIN") which correctly restricts /admin to ADMIN users.
  2. Step 2: Verify public access and deny others

    http
      .authorizeHttpRequests(auth -> auth
        .requestMatchers("/admin").hasRole("ADMIN")
        .requestMatchers("/public").permitAll()
        .anyRequest().denyAll()
      )
      .formLogin();
    permits all to /public and denies all other requests, matching the requirement.
  3. Final Answer:

    http
      .authorizeHttpRequests(auth -> auth
        .requestMatchers("/admin").hasRole("ADMIN")
        .requestMatchers("/public").permitAll()
        .anyRequest().denyAll()
      )
      .formLogin();
    -> Option A
  4. Quick Check:

    hasRole ADMIN + permitAll public + deny others =
    http
      .authorizeHttpRequests(auth -> auth
        .requestMatchers("/admin").hasRole("ADMIN")
        .requestMatchers("/public").permitAll()
        .anyRequest().denyAll()
      )
      .formLogin();
    [OK]
Hint: Use hasRole("ADMIN") for admin, permitAll for public [OK]
Common Mistakes:
  • Swapping roles and permissions for paths
  • Allowing public access to admin paths
  • Using hasAuthority instead of hasRole without prefix