Bird
Raised Fist0
Spring Bootframework~20 mins

Role-based access control in Spring Boot - Practice Problems & Coding Challenges

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
🎖️
Role-based Access Control Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing a secured endpoint without the required role?

Consider a Spring Boot REST controller method secured with @PreAuthorize("hasRole('ADMIN')"). What happens if a user without the ADMIN role tries to access this endpoint?

Spring Boot
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AdminController {
    @GetMapping("/admin/data")
    @PreAuthorize("hasRole('ADMIN')")
    public String getAdminData() {
        return "Sensitive admin data";
    }
}
AThe user receives HTTP 403 Forbidden error.
BThe user receives HTTP 401 Unauthorized error.
CThe user receives HTTP 404 Not Found error.
DThe user receives the string "Sensitive admin data".
Attempts:
2 left
💡 Hint

Think about what HTTP status code means 'access denied' due to insufficient permissions.

📝 Syntax
intermediate
2:00remaining
Which annotation correctly restricts access to users with role USER or ADMIN?

In Spring Security, which @PreAuthorize expression correctly allows access only to users with role USER or ADMIN?

A@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
B@PreAuthorize("hasRole('USER') and hasRole('ADMIN')")
C@PreAuthorize("hasRole('USER', 'ADMIN')")
D@PreAuthorize("hasAnyRole('USER', 'ADMIN')")
Attempts:
2 left
💡 Hint

Look for the expression that checks for multiple roles in one call.

state_output
advanced
2:00remaining
What is the role of SecurityContextHolder in Spring Security?

After a user logs in, Spring Security stores authentication details. What does SecurityContextHolder.getContext().getAuthentication() return?

AAn Authentication object containing the user's principal, credentials, and granted authorities.
BA boolean indicating if the user is authenticated.
CThe user's password in plain text.
DA list of all users currently logged in.
Attempts:
2 left
💡 Hint

Think about what information Spring Security keeps about the current user.

🔧 Debug
advanced
2:00remaining
Why does this method allow access to all users despite @PreAuthorize annotation?

Given the following Spring Boot controller method, why does it allow access to users without the ADMIN role?

Spring Boot
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {
    @GetMapping("/secure-data")
    @PreAuthorize("hasRole('ADMIN')")
    public String getData() {
        return "Secure Data";
    }
}
AThe @PreAuthorize annotation is misspelled.
BThe user actually has the ADMIN role but it is not visible.
CMethod security is not enabled in the Spring Boot application.
DThe endpoint URL is incorrect.
Attempts:
2 left
💡 Hint

Check if method security annotations are activated in the configuration.

🧠 Conceptual
expert
3:00remaining
How does Spring Security differentiate roles internally when using hasRole vs hasAuthority?

In Spring Security, what is the key difference between hasRole('ADMIN') and hasAuthority('ADMIN') in access control expressions?

A<code>hasAuthority</code> automatically prefixes the role name with 'ROLE_', while <code>hasRole</code> checks the exact authority string.
B<code>hasRole</code> automatically prefixes the role name with 'ROLE_', while <code>hasAuthority</code> checks the exact authority string.
CThere is no difference; both check the exact same authority string.
D<code>hasRole</code> checks user permissions, <code>hasAuthority</code> checks user groups.
Attempts:
2 left
💡 Hint

Think about how Spring Security stores roles internally with prefixes.

Practice

(1/5)
1. What is the main purpose of role-based access control (RBAC) in a Spring Boot application?
easy
A. To restrict access to resources based on user roles
B. To improve application performance by caching data
C. To automatically generate user interfaces
D. To handle database transactions efficiently

Solution

  1. Step 1: Understand RBAC concept

    RBAC limits access to parts of an application depending on the roles assigned to users.
  2. Step 2: Identify the purpose in Spring Boot

    In Spring Boot, RBAC is used to protect resources by checking user roles before allowing access.
  3. Final Answer:

    To restrict access to resources based on user roles -> Option A
  4. Quick Check:

    RBAC controls access by roles = A [OK]
Hint: RBAC controls who can do what by roles [OK]
Common Mistakes:
  • Confusing RBAC with performance optimization
  • Thinking RBAC generates UI automatically
  • Mixing RBAC with database transaction handling
2. Which annotation is used in Spring Boot to enforce role-based access control on a method?
easy
A. @PreAuthorize
B. @Autowired
C. @RequestMapping
D. @Entity

Solution

  1. Step 1: Identify role enforcement annotation

    Spring Security uses @PreAuthorize to check roles before method execution.
  2. Step 2: Differentiate from other annotations

    @RequestMapping handles URL mapping, @Autowired injects beans, @Entity marks database entities.
  3. Final Answer:

    @PreAuthorize -> Option A
  4. Quick Check:

    @PreAuthorize controls access by roles [OK]
Hint: Use @PreAuthorize to check roles on methods [OK]
Common Mistakes:
  • Using @RequestMapping for access control
  • Confusing @Autowired with security annotations
  • Mistaking @Entity for access control
3. Given the method annotation @PreAuthorize("hasRole('ADMIN')"), what will happen if a user with role USER tries to access this method?
medium
A. Access is granted because USER is a valid role
B. Method throws a syntax error
C. Access is denied because the user lacks ADMIN role
D. Access is granted only if the user is authenticated

Solution

  1. Step 1: Understand the @PreAuthorize expression

    The expression requires the user to have the ADMIN role to access the method.
  2. Step 2: Check user role against requirement

    A user with only USER role does not meet the ADMIN role requirement, so access is denied.
  3. Final Answer:

    Access is denied because the user lacks ADMIN role -> Option C
  4. Quick Check:

    Role check fails without ADMIN role = B [OK]
Hint: User must have exact role in @PreAuthorize to access [OK]
Common Mistakes:
  • Assuming any authenticated user can access
  • Thinking USER role is enough for ADMIN-only methods
  • Confusing syntax error with access denial
4. Consider this method in a Spring Boot controller:
@PreAuthorize("hasRole('ADMIN')")
public String adminPage() {
    return "Welcome Admin";
}

What is the likely cause if users with ADMIN role still get access denied errors?
medium
A. The method must be static to work with @PreAuthorize
B. The role prefix 'ROLE_' is missing in the role check
C. The return type should be ResponseEntity<String>
D. The method should be annotated with @GetMapping instead

Solution

  1. Step 1: Understand Spring Security role prefix

    Spring Security by default adds 'ROLE_' prefix to roles internally.
  2. Step 2: Check role naming in @PreAuthorize

    Using hasRole('ADMIN') expects the granted authority to be 'ROLE_ADMIN'. If roles lack this prefix, access is denied.
  3. Final Answer:

    The role prefix 'ROLE_' is missing in the role check -> Option B
  4. Quick Check:

    Missing 'ROLE_' prefix causes access denial = D [OK]
Hint: Remember Spring Security adds 'ROLE_' prefix by default [OK]
Common Mistakes:
  • Thinking @GetMapping affects access control
  • Believing return type affects security
  • Assuming method must be static for @PreAuthorize
5. You want to restrict access to a service method so that only users with either ADMIN or MANAGER roles can call it. Which @PreAuthorize expression correctly enforces this?
hard
A. @PreAuthorize("hasRole('ADMIN,MANAGER')")
B. @PreAuthorize("hasRole('ADMIN', 'MANAGER')")
C. @PreAuthorize("hasRole('ADMIN') and hasRole('MANAGER')")
D. @PreAuthorize("hasAnyRole('ADMIN', 'MANAGER')")

Solution

  1. Step 1: Understand role checks for multiple roles

    To allow access if user has either ADMIN or MANAGER, use hasAnyRole('ADMIN', 'MANAGER').
  2. Step 2: Analyze each option

    @PreAuthorize("hasRole('ADMIN') and hasRole('MANAGER')") requires both roles (AND), which is too strict. @PreAuthorize("hasRole('ADMIN', 'MANAGER')") is invalid as hasRole accepts only one role. @PreAuthorize("hasAnyRole('ADMIN', 'MANAGER')") uses hasAnyRole which is concise and correct. @PreAuthorize("hasRole('ADMIN,MANAGER')") is invalid syntax.
  3. Final Answer:

    @PreAuthorize("hasAnyRole('ADMIN', 'MANAGER')") -> Option D
  4. Quick Check:

    Use hasAnyRole for multiple allowed roles = A [OK]
Hint: Use hasAnyRole for OR conditions on roles [OK]
Common Mistakes:
  • Using AND instead of OR for multiple roles
  • Passing multiple roles as a single string
  • Not using hasAnyRole for multiple roles