0
0
Spring Bootframework~10 mins

Securing endpoints by role in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the role required to access the endpoint.

Spring Boot
@PreAuthorize("hasRole('[1]')")
@GetMapping("/admin")
public String adminEndpoint() {
    return "Admin content";
}
Drag options to blanks, or click blank then click option'
AGUEST
BMANAGER
CUSER
DADMIN
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase role names like 'admin' instead of uppercase 'ADMIN'.
Forgetting to use the @PreAuthorize annotation.
2fill in blank
medium

Complete the code to secure the endpoint so only users with the 'USER' role can access it.

Spring Boot
@PreAuthorize("hasRole('[1]')")
@GetMapping("/profile")
public String userProfile() {
    return "User profile content";
}
Drag options to blanks, or click blank then click option'
AMODERATOR
BADMIN
CUSER
DGUEST
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'user' instead of 'USER'.
Using a role that does not exist in the system.
3fill in blank
hard

Fix the error in the annotation to correctly check if the user has the 'MANAGER' role.

Spring Boot
@PreAuthorize("hasRole([1])")
@GetMapping("/manage")
public String manageEndpoint() {
    return "Manager content";
}
Drag options to blanks, or click blank then click option'
A"MANAGER"
BMANAGER
C'MANAGER'
Dmanager
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the role name.
Using single quotes inside double quotes without escaping.
4fill in blank
hard

Fill both blanks to secure the endpoint so only users with 'ADMIN' or 'MODERATOR' roles can access it.

Spring Boot
@PreAuthorize("hasRole('[1]') or hasRole('[2]')")
@GetMapping("/dashboard")
public String dashboard() {
    return "Dashboard content";
}
Drag options to blanks, or click blank then click option'
AADMIN
BUSER
CMODERATOR
DGUEST
Attempts:
3 left
💡 Hint
Common Mistakes
Using roles not allowed to access the endpoint.
Forgetting to use 'or' between role checks.
5fill in blank
hard

Fill all three blanks to secure the endpoint so only users with 'ADMIN' role and 'ACTIVE' status can access it using SpEL expressions.

Spring Boot
@PreAuthorize("hasRole('[1]') and @userService.isActiveUser(authentication.name) == [2] and principal.enabled == [3]")
@GetMapping("/secure-data")
public String secureData() {
    return "Secure data content";
}
Drag options to blanks, or click blank then click option'
AUSER
Btrue
DADMIN
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'True' or 'FALSE' instead of lowercase booleans.
Using the wrong role name.