0
0
Spring Bootframework~10 mins

Method-level security 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 enable method-level security in a Spring Boot application.

Spring Boot
@Configuration
@Enable[1]
public class SecurityConfig {
}
Drag options to blanks, or click blank then click option'
AWebSecurity
BEnableSecurity
CMethodSecurity
DGlobalMethodSecurity
Attempts:
3 left
💡 Hint
Common Mistakes
Using @EnableWebSecurity instead of @EnableGlobalMethodSecurity
Misspelling the annotation name
Not adding any annotation
2fill in blank
medium

Complete the code to restrict access to the method to users with the role 'ADMIN'.

Spring Boot
@PreAuthorize("hasRole('[1]')")
public void deleteUser(Long id) {
    // method body
}
Drag options to blanks, or click blank then click option'
AUSER
BADMIN
CGUEST
DMANAGER
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ROLE_ADMIN' instead of 'ADMIN' inside hasRole
Using a role that does not exist
Forgetting to add @PreAuthorize
3fill in blank
hard

Fix the error in the method-level security annotation to allow access only if the user has 'USER' role and the id matches the authenticated user's id.

Spring Boot
@PreAuthorize("hasRole('[1]') and #id == authentication.principal.id")
public void updateProfile(Long id) {
    // method body
}
Drag options to blanks, or click blank then click option'
AADMIN
BGUEST
CUSER
DMANAGER
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ADMIN' role which is too restrictive
Using 'GUEST' role which is too permissive
Incorrect SpEL expression syntax
4fill in blank
hard

Fill both blanks to create a method that allows access only if the user has 'ADMIN' role or the user id matches the authenticated user's id.

Spring Boot
@PreAuthorize("hasRole('[1]') or #id [2] authentication.principal.id")
public void accessResource(Long id) {
    // method body
}
Drag options to blanks, or click blank then click option'
AADMIN
B==
C!=
DUSER
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'USER' role instead of 'ADMIN'
Using '!=' operator which denies access when ids match
Forgetting to use # before id in SpEL
5fill in blank
hard

Fill all three blanks to create a method that allows access only if the user has 'MANAGER' role and the department matches the authenticated user's department.

Spring Boot
@PreAuthorize("hasRole('[1]') and #dept [2] authentication.principal.[3]")
public void manageDepartment(String dept) {
    // method body
}
Drag options to blanks, or click blank then click option'
AMANAGER
B==
Cdepartment
DUSER
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'USER' role instead of 'MANAGER'
Using '!=' operator which denies access when departments match
Using wrong property name instead of 'department'