Can you explain how to use method-level security in Spring Boot?

Method-level security lets you attach access rules directly to individual service or controller methods using annotations, giving finer-grained control than URL-based rules alone.

Key Points

Example

A deleteUser() service method is annotated so only an ADMIN can invoke it, even if it's called internally from another service rather than directly through a controller.

@EnableMethodSecurity
@Configuration
public class MethodSecurityConfig {}

@Service
public class UserService {

    @PreAuthorize("hasRole('ADMIN')")
    public void deleteUser(Long userId) {
        // deletion logic
    }
}

Interview Tip

A concise interview answer is

I enable method-level security with @EnableMethodSecurity, then annotate individual service methods with @PreAuthorize using SpEL expressions like hasRole('ADMIN'). This protects the method itself regardless of how it's invoked, which is more robust than relying only on URL-based rules at the controller boundary.

Last updated: 16 July 2026

Previous question: How can you implement authentication and authorization in Spring Boot?

Next question: Can you describe an approach to implement security in service-to-service communication?

More Spring Security - Level II interview questions

All Spring Security - Level II questions