Bird
Raised Fist0
Spring Bootframework~20 mins

Why Spring Security matters in Spring Boot - Challenge 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
Challenge - 5 Problems
🎖️
Spring Security Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use Spring Security in a web application?

Which of the following best explains why Spring Security is important in a web application?

AIt automatically generates user interfaces for all pages without coding.
BIt provides a framework to handle authentication and authorization securely.
CIt speeds up database queries by caching results.
DIt manages the deployment of the application to cloud servers.
Attempts:
2 left
💡 Hint

Think about what security means for users accessing a web app.

component_behavior
intermediate
2:00remaining
What happens when Spring Security is added to a Spring Boot app?

When you add Spring Security to a Spring Boot application without any extra configuration, what is the default behavior?

AOnly POST requests require authentication, GET requests are open.
BThe application crashes due to missing security settings.
CAll HTTP requests are allowed without restrictions.
DAll HTTP requests require authentication with a generated default user and password.
Attempts:
2 left
💡 Hint

Think about what Spring Security does out of the box to protect your app.

state_output
advanced
2:30remaining
Spring Security filter chain behavior

Consider this Spring Security filter chain setup:

http
  .authorizeHttpRequests(auth -> auth
    .requestMatchers("/public/**").permitAll()
    .anyRequest().authenticated())
  .formLogin(withDefaults());

What happens when an unauthenticated user tries to access /public/info and /private/data?

A/public/info is accessible without login; /private/data requires login.
BBoth URLs require login before access.
C/public/info redirects to login; /private/data is accessible without login.
DBoth URLs are accessible without login.
Attempts:
2 left
💡 Hint

Look at which paths are allowed without authentication.

📝 Syntax
advanced
2:30remaining
Identify the correct Spring Security configuration snippet

Which of the following code snippets correctly configures Spring Security to disable CSRF protection and allow all requests to /api/** without authentication?

A
http.csrf().disable()
  .authorizeHttpRequests(auth -> auth
    .requestMatchers("/api/**").permitAll()
    .anyRequest().authenticated());
B
http.csrf().enable()
  .authorizeHttpRequests(auth -> auth
    .requestMatchers("/api/**").permitAll()
    .anyRequest().authenticated());
C
http.csrf().disable()
  .authorizeHttpRequests(auth -> auth
    .requestMatchers("/api/**").authenticated()
    .anyRequest().permitAll());
D
http.csrf().disable()
  .authorizeHttpRequests(auth -> auth
    .requestMatchers("/api/**").denyAll()
    .anyRequest().authenticated());
Attempts:
2 left
💡 Hint

Check the method to disable CSRF and the correct order of authorization rules.

🔧 Debug
expert
3:00remaining
Why does this Spring Security configuration cause a runtime error?

Given this Spring Security configuration method:

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  http
    .authorizeHttpRequests(auth -> auth
      .requestMatchers("/admin/**").hasRole("ADMIN")
      .anyRequest().authenticated())
    .formLogin();
  return http.build();
}

When running the app, it throws an error: java.lang.IllegalArgumentException: Role prefix 'ROLE_' is missing. What is the cause?

AThe requestMatchers() method requires a list of URLs, not a string.
BThe SecurityFilterChain bean must be named 'securityFilterChain' exactly.
CThe role name should be prefixed with 'ROLE_' in hasRole method argument.
DThe formLogin() method must be called before authorizeHttpRequests().
Attempts:
2 left
💡 Hint

Think about how Spring Security expects role names internally.

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