EnableGlobalMethodSecurity vs EnableWebSecurity
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
These two annotations solve different problems in Spring Security. @EnableWebSecurity turns on web-request security infrastructure, while @EnableGlobalMethodSecurity enables method-level annotations such as @PreAuthorize and @Secured. In current Spring Security, the method-security annotation is usually @EnableMethodSecurity, which supersedes @EnableGlobalMethodSecurity.
@EnableWebSecurity Is About HTTP Requests
@EnableWebSecurity activates the web security configuration that protects incoming HTTP requests. This is where you define things such as:
- which URLs require authentication
- which roles can access certain paths
- login, logout, and CSRF behavior
- which
SecurityFilterChainbeans apply
A modern example looks like this:
That configuration governs request-level authorization before controller methods even run.
Method Security Is a Different Layer
Method security protects individual Spring-managed methods, usually at the service layer. It answers questions like:
- can this method be called by the current user
- can this result be returned to the caller
- should a parameter list be filtered
Historically, this was enabled with @EnableGlobalMethodSecurity. In newer Spring Security versions, @EnableMethodSecurity is the recommended replacement.
This protects the method no matter who calls it, including internal application code that bypasses the controller layer.
So Which One Do You Need
If you only secure HTTP routes, @EnableWebSecurity is the main switch. If you also want annotations on services or repositories, add method security too.
That means the real comparison is not "one versus the other" so much as "request-level security versus method-level security."
A common application uses both:
- '
@EnableWebSecurityfor URL rules and filter-chain behavior' - '
@EnableMethodSecurityfor@PreAuthorize,@Secured, or JSR-250 annotations'
Why Method Security Still Matters
Relying only on URL rules is often not enough. The same service method may be called:
- from a REST controller
- from another internal component
- from scheduled jobs or messaging endpoints
Method security keeps the authorization rule attached to the business operation instead of assuming every path into the code comes through the same web controller.
Legacy Note: @EnableGlobalMethodSecurity
You will still see @EnableGlobalMethodSecurity(prePostEnabled = true) in older code:
That is the older mechanism. Current Spring Security documentation recommends @EnableMethodSecurity, which supersedes it and provides a more modern internal implementation.
Common Pitfalls
The biggest pitfall is expecting @EnableWebSecurity alone to activate @PreAuthorize and related method annotations. It does not.
Another common mistake is treating method security as a replacement for web security. The two layers protect different entry points and often complement each other.
In practice, teams usually get the most predictable results when request rules and service-method rules are aligned instead of treated as unrelated systems.
In modern projects, it is also easy to copy old @EnableGlobalMethodSecurity examples without realizing that @EnableMethodSecurity is now the preferred annotation.
Summary
- '
@EnableWebSecurityenables request-level web security infrastructure.' - '
@EnableGlobalMethodSecuritywas the older way to enable method-level annotations.' - In current Spring Security,
@EnableMethodSecurityis the preferred method-security annotation. - Web security protects URLs and filter chains, while method security protects service and bean methods.
- Many real applications use both layers together.
Related reading
- Encoded password does not look like BCrypt
- Encrypt and decrypt a string in C?
- Encrypt and decrypt using PyCrypto AES-256
- Encrypting the Hadoop Distributed Cache file
- EnableTransactionManagement in Spring Boot
- Encoding as Base64 in Java
- Enforce MFA for AWS console login but not for API calls
- Error certificate signed by unknown authority after switching GCP project

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.