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.
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
}
}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