Quick revision
The short answer
The causes to check, in order, are: method security is not enabled because @EnableMethodSecurity is missing; the method is called from another method in the same class, so the call never passes through the proxy; the method is private or final, or the object is not a Spring bean at all, so no proxy can intercept it; or the team used @Secured or @RolesAllowed without turning on securedEnabled or jsr250Enabled. Whatever we find, we confirm the fix with a test that calls the method as a non-admin user and expects AccessDeniedException.
What to remember
In this answer, we traced every cause of an ignored @PreAuthorize to the proxy that enforces it. Method security may not be enabled; the call may come from the same class and never pass the proxy; the method may be private or final, or the object not a Spring bean; or @Secured and @RolesAllowed may be used without their switches. We fixed each one and confirmed it with a test that calls the real entry point as a non-admin and expects AccessDeniedException.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The causes to check, in order, are: method security is not enabled because @EnableMethodSecurity is missing; the method is called from another method in the same class, so the call never passes through the proxy; the method is private or final, or the object is not a Spring bean at all, so no proxy can intercept it; or the team used @Secured or @RolesAllowed without turning on securedEnabled or jsr250Enabled. Whatever we find, we confirm the fix with a test that calls the method as a non-admin user and expects AccessDeniedException.
Why this question is asked
An annotation that looks like protection but does nothing is one of the most dangerous bugs in a codebase, because reviewers see it and stop worrying. Interviewers ask this question to see whether we know how method security is wired, since every cause on the list follows from one fact: the check lives in a proxy, and anything that avoids the proxy avoids the check. In this answer, we will recap how the proxy works, then walk through each cause with a reproduction and a fix, and finish with the test that proves the method is really protected.
1A Quick Recap: The Check Lives in a Proxy
@PreAuthorize is only metadata. Something must read it and act on it. When method security is enabled, Spring registers an interceptor and, for every bean with secured methods, creates a proxy, a generated wrapper object that other beans receive instead of the real one. When a call arrives at the proxy, the interceptor evaluates the expression and either throws AccessDeniedException or passes the call to the real object.
So for the check to run, three things must be true: method security must be enabled, the object must be a Spring bean wrapped in a proxy, and the call must enter through that proxy. Each cause below breaks one of these.
- @EnableMethodSecurity present?YesContinueNoAnnotation ignored
- Object is a Spring bean?YesContinueNoNo proxy, no check
- Method public and not final?YesContinueNoProxy cannot intercept
- Call comes from another bean?YesCheck runsNothis.call() skips the proxy
4 steps in order. Play them, or step through them one at a time.
2Cause 1: Method Security Is Not Enabled
The simplest cause, and surprisingly common after an upgrade or a refactor that deleted an old configuration class. Without @EnableMethodSecurity, Spring Boot does not turn method security on by itself, and @PreAuthorize is silently ignored.
A quick way to check at runtime is to log the class of the injected bean, for example getClass().getName(). A proxied bean shows a name containing $$SpringCGLIB$$ (or a JDK proxy class); a plain class name means nothing is wrapping it, either because security is off or because no other aspect applies. We can also check that the code base does not still rely on the old @EnableGlobalMethodSecurity in a module that is no longer loaded.
3Cause 2: A Call From Inside the Same Class
The controller may call a public method without the annotation, which then calls the annotated method on this.
The proxy only sees the first call, to handle(), which has no annotation. The inner call goes straight from the real object to itself. The fix is to annotate the method the controller actually calls, or to move deleteUser() into a separate bean so the call crosses a proxy. Injecting the bean into itself also works, but it hides the problem and makes the code harder to follow, so I avoid it.
4Cause 3: Private, Final or Not a Bean
Spring's class-based proxies work by generating a subclass and overriding methods. A private method cannot be overridden, and neither can a final method or any method of a final class, so annotations on them are ignored. Kotlin classes are final by default, which is why Kotlin projects use the all-open (Spring) compiler plugin. Keeping secured methods public is the simplest rule.
The other variant is an object Spring never managed. If someone writes new UserAdminService(repo) inside a controller or a static helper, that instance has no proxy at all, however correct its annotations. The fix is to inject the bean instead of constructing it.
5Cause 4: The Wrong Annotation Family Switched Off
@EnableMethodSecurity enables only the pre and post annotations by default. If a team used @Secured("ROLE_ADMIN") or the Jakarta annotation @RolesAllowed("ADMIN"), those are ignored unless switched on explicitly.
While here, it is worth checking the expression itself. hasRole('ADMIN') looks for the authority ROLE_ADMIN. That mistake usually causes the opposite symptom, admins being refused, but an expression such as hasRole('ADMIN') or isAuthenticated() written in a hurry can grant everyone access too.
6Proving the Fix With a Test
Every cause above is invisible in a manual test done as an admin. So we write the test that fails today and passes after the fix: call the method, through the real Spring bean, as a user without the role.
If the bug was self-invocation, the test should call the entry point the controller uses, here handle(), not only the annotated method; otherwise the test passes while the real path stays open. An end-to-end MockMvc test against the endpoint is the strongest proof, because it exercises the exact route an attacker would use.
7Follow-up questions
Would URL rules have prevented this?
Partly. A rule such as requestMatchers("/admin/**").hasRole("ADMIN") protects the endpoint regardless of method security. That is why critical operations often get both: the URL rule as a coarse guard and method security as a second line close to the logic.
Does putting @PreAuthorize on an interface method work?
Yes, Spring Security finds annotations on interfaces as well as on classes. The same proxy rules apply: the call must come through the bean, not from inside the implementation.
Could the order of aspects matter?
It can when several aspects wrap the same method, for example a transaction and method security. Method security normally runs early, so a denied call does not even start a transaction. If someone changed advisor ordering, a denied call might do work first, which is worth checking in the same investigation.
8Conclusion
In this answer, we traced every cause of an ignored @PreAuthorize to the proxy that enforces it. Method security may not be enabled; the call may come from the same class and never pass the proxy; the method may be private or final, or the object not a Spring bean; or @Secured and @RolesAllowed may be used without their switches. We fixed each one and confirmed it with a test that calls the real entry point as a non-admin and expects AccessDeniedException.
Open with
Since method security works through a proxy, I would check everything that could keep the call away from it. First, whether @EnableMethodSecurity is actually present in a loaded configuration; without it the annotation is just ignored.
Then, if they want more
A quick check is to log the injected bean's class name and see whether it is a CGLIB proxy. Second, self-invocation: if the controller calls a public method without the annotation, and that method calls the annotated one on this, the call never passes through the proxy, so the check never runs.
The fix is to annotate the entry point or move the protected method to another bean. Third, the method must be public and not final, and the object must be a Spring bean; a private or final method cannot be intercepted, and an instance created with new has no proxy at all.
Fourth, if the code uses @Secured or @RolesAllowed rather than @PreAuthorize, those need securedEnabled or jsr250Enabled set to true.
Whatever the cause, I would prove the fix with a test that calls the real path as a user with only ROLE_USER and expects AccessDeniedException, ideally through MockMvc expecting a 403, because testing as an admin passes even when the protection is missing.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A @PreAuthorize("hasRole('ADMIN')") check on a service method is not enforced: any user...": https://scrivio-interview-library.netlify.app/spring-boot/authorization/1183e9a6b4/