6.16 Method-Level Security (@EnableMethodSecurity, @PreAuthorize) (Week 6 • Spring Security 6)
1. Core Concept & Internal Working
Method-Level Security (@EnableMethodSecurity, @PreAuthorize) (Spring Security 6) covers `@EnableMethodSecurity`, SpEL expressions `@PreAuthorize("hasRole('ADMIN')")`, `@PreAuthorize("#userId == authentication.principal.userId or hasRole('ADMIN')")`. In production software engineering, it is used to Enforcing defense-in-depth authorization directly on `@Service` methods even if a new controller endpoint is added later. Mastering its underlying execution rules and edge-case boundaries lets you write reliable, maintainable code.
2. Real-World Engineering Use Case
Enforcing defense-in-depth authorization directly on `@Service` methods even if a new controller endpoint is added later.
3. Granular Subtopics Covered
- `@EnableMethodSecurity`: Core foundation of Method-Level Security (@EnableMethodSecurity, @PreAuthorize): Enforcing defense-in-depth authorization directly on `@Service` methods even if a new controller endpoint is added later.
- SpEL expressions `@PreAuthorize("hasRole('ADMIN')")`: Master 'SpEL expressions `@PreAuthorize("hasRole('ADMIN')")`' inside Method-Level Security (@EnableMethodSecurity, @PreAuthorize) across real-world inputs.
- `@PreAuthorize("#userId == authentication.principal.userId or hasRole('ADMIN')")`: Master '`@PreAuthorize("#userId == authentication.principal.userId or hasRole('ADMIN')")`' inside Method-Level Security (@EnableMethodSecurity, @PreAuthorize) across real-world inputs.
- Production Pitfall Guard (Method-Level Security (@EnableMethodSecurity, @PreAuthorize)): Prevent common defect: Using `@PreAuthorize` without enabling `@EnableMethodSecurity` on a `@Configuration` class (annotations are silently ignored!).
- Hands-On Verification (Method-Level Security (@EnableMethodSecurity, @PreAuthorize)): Add `@EnableMethodSecurity` and protect admin product mutations and customer-owned order lookups with `@PreAuthorize`.
- Method-Level Security (@EnableMethodSecurity, @PreAuthorize) — Interview Readiness: Explain behavior, trade-offs, and edge cases of Method-Level Security (@EnableMethodSecurity, @PreAuthorize).
4. Reference Implementation
@org.springframework.security.access.prepost.PreAuthorize("hasRole('ADMIN')")
public ProductResponse updatePrice(Long productId, BigDecimal newPrice) { /* ... */ }
5. Common Mistakes to Avoid
- Using `@PreAuthorize` without enabling `@EnableMethodSecurity` on a `@Configuration` class (annotations are silently ignored!).
- Skipping boundary, null/empty, or error-state verification when implementing Method-Level Security (@EnableMethodSecurity, @PreAuthorize).
- Pushing untested Method-Level Security (@EnableMethodSecurity, @PreAuthorize) changes without clean naming and Git commit context.
6. Hands-On Exercise
Add `@EnableMethodSecurity` and protect admin product mutations and customer-owned order lookups with `@PreAuthorize`.
7. Technical Interview Checkpoints
- Q: What problem does Method-Level Security (@EnableMethodSecurity, @PreAuthorize) solve in Spring Security 6, and what fails if it is misused?
Key Focus: Enforcing defense-in-depth authorization directly on `@Service` methods even if a new controller endpoint is added later. Guard against: Using `@PreAuthorize` without enabling `@EnableMethodSecurity` on a `@Configuration` class (annotations are silently ignored!). - Q: Explain the internal working of Method-Level Security (@EnableMethodSecurity, @PreAuthorize) (`@EnableMethodSecurity`, SpEL expressions `@PreAuthorize("hasRole('ADMIN')")`, `@PreAuthorize("#userId == authentication.principal.userId or hasRole('ADMIN')")`).
Key Focus: Walk step-by-step through `@EnableMethodSecurity`, SpEL expressions `@PreAuthorize("hasRole('ADMIN')")`, `@PreAuthorize("#userId == authentication.principal.userId or hasRole('ADMIN')")` with a concrete code example. - Q: How did you apply and verify Method-Level Security (@EnableMethodSecurity, @PreAuthorize) in your hands-on lab?
Key Focus: Add `@EnableMethodSecurity` and protect admin product mutations and customer-owned order lookups with `@PreAuthorize`.