What configuration steps are needed for method-level security in Spring?

Method-level security lets you enforce authorization rules directly on service or controller methods using annotations, rather than only at the URL level, giving finer-grained control over who can invoke specific business operations.

Key Points

Example

Annotating a deleteUser(id) service method with @PreAuthorize("hasRole('ADMIN')") ensures the method throws an AccessDeniedException if a non-admin user's request somehow reaches it, even if a URL-level check was missed upstream.

@Configuration
@EnableMethodSecurity
public class MethodSecurityConfig {
}

@PreAuthorize("hasRole('ADMIN')")
public void deleteUser(Long id) {
    userRepository.deleteById(id);
}

Interview Tip

A concise interview answer is

I enable it with @EnableMethodSecurity on a config class, then annotate individual methods with @PreAuthorize or @Secured to declare the required roles or expressions. This gives defense in depth — even if a URL-level rule is missed, the method itself refuses to execute for an unauthorized caller.

Last updated: 11 November 2026

Previous question: What are the common challenges when securing a Spring MVC application?

Next question: Explain the concept of dependency injection in the context of Spring MVC.

More Spring MVC (Optional) interview questions

All Spring MVC (Optional) questions