Complete the code to check if a user has the right role before accessing a resource.
if (authentication.getAuthorities().contains(new SimpleGrantedAuthority([1]))) { // allow access }
The code checks if the user has the ROLE_ADMIN authority to allow access.
Complete the code to restrict access to a method only to users with the ADMIN role using annotation.
@PreAuthorize([1])
public void deleteUser(Long id) {
// deletion logic
}permitAll() which allows everyoneThe @PreAuthorize annotation restricts method access to users with the ADMIN role.
Fix the error in the code that checks user roles in a service method.
if (authentication.getAuthorities().stream().anyMatch(auth -> auth.getAuthority().equals([1]))) { // proceed }
The authority string must include the 'ROLE_' prefix to match Spring Security roles correctly.
Fill in the blank to create a method that returns true if the user has the ADMIN role and false otherwise.
public boolean isAdmin(Authentication authentication) {
return authentication.getAuthorities().stream()
.anyMatch(auth -> auth.getAuthority().equals([1]));
}The method checks if the user has the ROLE_ADMIN authority.
Fill in the blanks to configure HTTP security to allow only ADMIN users to access '/admin/**' endpoints.
http.authorizeHttpRequests()
.requestMatchers([1])
.hasRole([2])
.anyRequest().authenticated();hasRole() which expects role without prefixThe code restricts access to URLs starting with '/admin/' to users with the ADMIN role.