You have a service layer in your application that contains methods that should only be accessed by certain roles. How would you implement method-level security using Spring Security annotations to restrict access to these methods based on user roles?

Restricting service-layer methods to specific roles is done by enabling method security and applying @PreAuthorize (or @Secured) annotations directly on the methods that require protection, so the check runs regardless of the calling path.

Key Points

Example

An adminOnlyMethod() in a service class is annotated with @PreAuthorize("hasRole('ADMIN')") so that even if a developer later adds a new controller calling it, the role check is enforced automatically.

@Service
public class ReportService {

    @PreAuthorize("hasRole('ADMIN')")
    public void adminOnlyMethod() {
        // logic for admin only
    }
}

Interview Tip

A concise interview answer is

I'd enable method security with @EnableGlobalMethodSecurity(prePostEnabled = true), then annotate the sensitive service methods with @PreAuthorize using role expressions like hasRole('ADMIN'). That keeps the check enforced at the method itself, so it applies no matter which controller or internal code path calls it.

Last updated: 16 July 2026

Previous question: You are building a web application that requires secure forms to prevent Cross-Site Request Forgery (CSRF) attacks. How would you configure CSRF protection in Spring Security, and what additional measures would you take to ensure form security?

Next question: In your application, you need to securely store user passwords. What approach would you take to implement password encoding in Spring Security? Discuss the choice of encoding algorithm and how to verify passwords during authentication.

More Spring Security - Level II interview questions

All Spring Security - Level II questions