Spring Security, Method Security annotation Secured is not working java config
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
@Secured failures in Spring projects are usually configuration issues, not annotation bugs. Method security depends on proxy based interception, so one missing switch can make protected methods execute without checks. The fix is to enable the right method security mode and verify call paths actually pass through a Spring proxy.
Enabling Method Security in Modern Spring
For Spring Boot 3 and Spring Security 6, use @EnableMethodSecurity with securedEnabled = true.
For older stacks, the equivalent was @EnableGlobalMethodSecurity(securedEnabled = true). If you migrated and kept only the old annotation in incompatible versions, security advice may never activate.
Correct Role Mapping with @Secured
@Secured expects role style authorities, usually prefixed with ROLE_.
If your authentication stores authorities like ADMIN without the prefix, access checks will fail. Either store ROLE_ADMIN or customize authority mapping consistently.
Web Security Configuration Example
A minimal security filter chain helps verify authentication plus method authorization together.
When this is active, calling a @Secured("ROLE_ADMIN") method as user should return forbidden.
Proxy Boundaries and Self Invocation
The most common hidden issue is self invocation. If one method in a bean calls another secured method in the same bean using this, the call bypasses proxy interception and security advice does not run.
Bad pattern:
Better pattern is splitting secured logic into a second bean and calling that bean through Spring injection.
Testing Method Security
Add integration tests that assert both allowed and denied paths. This catches config regressions early.
Troubleshooting Checklist for Real Projects
When behavior still looks wrong, turn on Spring Security debug logs and inspect authentication authorities at runtime. Many teams spend hours on annotations while the real issue is an empty security context in async execution or scheduled jobs.
If secured methods run from @Async or scheduler threads, propagate security context intentionally or authenticate explicitly for that workflow. Method checks only run when an authentication object is present for the active thread.
Use this level during investigation and reduce log verbosity afterward to avoid noisy production output.
Common Pitfalls
- Forgetting
securedEnabled = true. Fix by enabling method security explicitly. - Using wrong role format. Fix by aligning
@Securedvalues and granted authorities with theROLE_convention. - Calling secured methods inside the same bean. Fix by moving secured logic to another injected bean.
- Relying only on controller tests. Fix by adding service level method security tests.
- Mixing legacy and modern configuration styles inconsistently. Fix by standardizing configuration for your Spring version.
Summary
@Securedworks when method security is enabled and calls pass through proxies.- Role naming must match granted authorities.
- Self invocation bypasses security interception.
- Integration tests should assert allowed and denied access paths.
- Version aligned configuration is essential after framework upgrades.
Related reading
- Spring Test returning 401 for unsecured URLs
- SpringBoot 401 UnAuthorized even with out security
- Springboot endpoint 403 OPTIONS when doing a POST request
- SQL injection that gets around mysql_real_escape_string
- Spring server.forward-headers-strategy NATIVE vs FRAMEWORK
- Spring spring.profiles.include overrides
- SpringApplicationConfiguration not found Erroneous spring-boot-starter-test content?
- SpringBoot - BeanDefinitionOverrideException Invalid bean definition

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.