Complete the code to specify the role required to access the method.
@PreAuthorize("hasRole('[1]')") public void adminOnlyMethod() { // method logic }
The @PreAuthorize annotation checks if the user has the specified role. Here, ADMIN is the role required.
Complete the code to define a method accessible only by users with the 'USER' role.
@PreAuthorize("hasRole('[1]')") public String userDashboard() { return "Welcome User"; }
The method is restricted to users with the USER role using @PreAuthorize.
Fix the error in the annotation to correctly check for the 'MANAGER' role.
@PreAuthorize("hasRole([1])") public void managerTask() { // task logic }
The role name must be a string inside double quotes within the annotation expression.
Fill both blanks to create a method accessible by either 'ADMIN' or 'MODERATOR' roles.
@PreAuthorize("hasRole('[1]') or hasRole('[2]')") public void adminOrModTask() { // logic here }
The method allows access if the user has either the ADMIN or MODERATOR role.
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.
@PreAuthorize("hasRole('[1]') and #[2].owner == authentication.name") public void secureResourceAccess(Resource [2]) { System.out.println([3].getId()); }
The method requires the ADMIN role and checks if the resource owner matches the logged-in user. The parameter and usage use res as the variable name.