Complete the code to restrict access to the method only to users with the role 'ADMIN'.
@PreAuthorize("hasRole('[1]')") public void adminOnlyMethod() { // method logic }
The @PreAuthorize annotation uses hasRole('ROLE_NAME') to restrict access. Here, only users with the role ADMIN can access the method.
Complete the code to allow access only if the user has the authority 'WRITE_PRIVILEGE'.
@PreAuthorize("hasAuthority('[1]')") public void writeData() { // method logic }
The hasAuthority expression checks if the user has a specific permission or authority. Here, only users with WRITE_PRIVILEGE can access the method.
Fix the error in the code to correctly restrict access to users with role 'MANAGER'.
@PreAuthorize("hasRole([1])") public void managerTask() { // method logic }
The role name must be a string literal inside quotes in the hasRole expression. Using single quotes 'MANAGER' is correct.
Fill both blanks to restrict access to users who have either 'ADMIN' role or 'WRITE_PRIVILEGE' authority.
@PreAuthorize("[1] or [2]") public void adminOrWriteAccess() { // method logic }
The expression uses hasRole('ADMIN') or hasAuthority('WRITE_PRIVILEGE') to allow access if either condition is true.
Fill all three blanks to restrict access to users with role 'USER' and authority 'READ_PRIVILEGE', but not with role 'GUEST'.
@PreAuthorize("[1] and [2] and not [3]") public void userReadAccess() { // method logic }
not for exclusion.The expression ensures the user has the USER role and READ_PRIVILEGE authority, but excludes users with the GUEST role.