0
0
Spring Bootframework~10 mins

Role-based access control 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 method.

Spring Boot
@PreAuthorize("hasRole('[1]')")
public void adminOnlyMethod() {
    // method logic
}
Drag options to blanks, or click blank then click option'
AADMIN
BMANAGER
CGUEST
DUSER
Attempts:
3 left
💡 Hint
Common Mistakes
Using a role that does not exist in the system.
Forgetting to use uppercase for role names.
2fill in blank
medium

Complete the code to define a method accessible only by users with the 'USER' role.

Spring Boot
@PreAuthorize("hasRole('[1]')")
public String userDashboard() {
    return "Welcome User";
}
Drag options to blanks, or click blank then click option'
AADMIN
BUSER
CGUEST
DMODERATOR
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ADMIN' role instead of 'USER'.
Misspelling the role name.
3fill in blank
hard

Fix the error in the annotation to correctly check for the 'MANAGER' role.

Spring Boot
@PreAuthorize("hasRole([1])")
public void managerTask() {
    // task logic
}
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 instead of double quotes inside the annotation.
4fill in blank
hard

Fill both blanks to create a method accessible by either 'ADMIN' or 'MODERATOR' roles.

Spring Boot
@PreAuthorize("hasRole('[1]') or hasRole('[2]')")
public void adminOrModTask() {
    // logic here
}
Drag options to blanks, or click blank then click option'
AADMIN
BUSER
CMODERATOR
DGUEST
Attempts:
3 left
💡 Hint
Common Mistakes
Using roles that are too general like 'USER' or 'GUEST'.
Forgetting to add both roles in the expression.
5fill in blank
hard

Fill all three blanks to create a method that allows access only if the user has 'ADMIN' role and the resource owner matches the current user.

Spring Boot
@PreAuthorize("hasRole('[1]') and #[2].owner == authentication.name")
public void secureResourceAccess(Resource [2]) {
    System.out.println([3].getId());
}
Drag options to blanks, or click blank then click option'
AADMIN
Bres
DUSER
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in parameter and method body.
Using a role other than 'ADMIN' for this strict check.