0
0
Spring Bootframework~10 mins

@PreAuthorize annotation 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 restrict access to the method only to users with the role 'ADMIN'.

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 name that does not exist in the system.
Forgetting to wrap the role name in quotes inside hasRole.
2fill in blank
medium

Complete the code to allow access only if the user has the authority 'WRITE_PRIVILEGE'.

Spring Boot
@PreAuthorize("hasAuthority('[1]')")
public void writeData() {
    // method logic
}
Drag options to blanks, or click blank then click option'
AWRITE_PRIVILEGE
BREAD_PRIVILEGE
CDELETE_PRIVILEGE
DEXECUTE_PRIVILEGE
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing roles with authorities.
Using an authority that the user does not have.
3fill in blank
hard

Fix the error in the code to correctly restrict access to users with role 'MANAGER'.

Spring Boot
@PreAuthorize("hasRole([1])")
public void managerTask() {
    // method logic
}
Drag options to blanks, or click blank then click option'
AMANAGER
B"MANAGER"
CROLE_MANAGER
D'MANAGER'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the role name.
Using double quotes inside double quotes without escaping.
4fill in blank
hard

Fill both blanks to restrict access to users who have either 'ADMIN' role or 'WRITE_PRIVILEGE' authority.

Spring Boot
@PreAuthorize("[1] or [2]")
public void adminOrWriteAccess() {
    // method logic
}
Drag options to blanks, or click blank then click option'
AhasRole('ADMIN')
BhasAuthority('READ_PRIVILEGE')
ChasAuthority('WRITE_PRIVILEGE')
DhasRole('USER')
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up roles and authorities in the expression.
Using 'and' instead of 'or' when both conditions are alternatives.
5fill in blank
hard

Fill all three blanks to restrict access to users with role 'USER' and authority 'READ_PRIVILEGE', but not with role 'GUEST'.

Spring Boot
@PreAuthorize("[1] and [2] and not [3]")
public void userReadAccess() {
    // method logic
}
Drag options to blanks, or click blank then click option'
AhasRole('USER')
BhasAuthority('READ_PRIVILEGE')
ChasRole('GUEST')
DhasAuthority('WRITE_PRIVILEGE')
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use not for exclusion.
Mixing roles and authorities incorrectly.