Method-level security bảo vệ từng method bằng annotation thay vì chỉ ở URL level. Bật bằng @EnableMethodSecurity (thay @EnableGlobalMethodSecurity đã deprecated).
@PreAuthorize — kiểm tra trước khi method chạy (dùng SpEL):
java
@PreAuthorize("hasRole('ADMIN')")
void deleteOrder(Long id) { ... }
@PreAuthorize("#orderId == authentication.principal.id or hasRole('ADMIN')") // ownership
Order getOrder(Long orderId) { ... }@PostAuthorize — kiểm tra sau khi method chạy (check return value): @PostAuthorize("returnObject.owner == authentication.name").@Secured("ROLE_ADMIN") — đơn giản hơn, không SpEL.
Lưu ý: @PreAuthorize (SpEL) linh hoạt nhất, recommended; chạy qua AOP proxy → self-invocation không được bảo vệ.