Method-Level Security Interview Questions and Answers
Top 10 Method-Level Security interview questions with answers, diagrams, Spring Security examples, production scenarios, and enterprise best practices.
Method-Level Security is one of the most powerful authorization features provided by Spring Security. Instead of securing only URLs, Method-Level Security protects individual service or controller methods, ensuring that business logic is executed only by authorized users.
In enterprise applications, URL security alone is not sufficient because the same business method may be invoked through multiple APIs, schedulers, messaging systems, or internal services.
Q1. What is Method-Level Security?
Answer
Method-Level Security is a mechanism that secures individual methods using authorization rules.
Instead of protecting only HTTP endpoints, Spring Security checks authorization before executing a method.
Request Flow
Client
│
HTTP Request
│
Spring Security Filter
│
Controller
│
Method Security Check
│
Business Service
│
Database
Benefits
- Fine-grained authorization
- Business-layer protection
- Reusable security
- Better maintainability
Q2. Why do we need Method-Level Security?
Answer
URL-based security protects endpoints.
However, business methods may be called from:
- REST APIs
- GraphQL APIs
- Scheduled Jobs
- Kafka Consumers
- Internal Services
- Batch Jobs
Method-Level Security ensures authorization regardless of how the method is invoked.
Example
Transfer Money()
↓
Authorized User?
↓
Yes
↓
Execute
Q3. How do you enable Method-Level Security in Spring Boot?
Answer
Method-Level Security is enabled using:
@EnableMethodSecurity
@Configuration
public class SecurityConfig {
}
Once enabled, Spring Security intercepts secured methods before execution.
Internal Flow
Application Starts
│
@EnableMethodSecurity
│
Spring Creates Proxies
│
Authorization Interceptor
│
Secured Methods
Q4. What annotations are used for Method-Level Security?
Answer
Spring Security provides several annotations.
| Annotation | Purpose |
|---|---|
@PreAuthorize |
Check authorization before execution |
@PostAuthorize |
Check authorization after execution |
@PreFilter |
Filter method arguments |
@PostFilter |
Filter returned collections |
@Secured |
Role-based authorization |
@RolesAllowed |
JSR-250 role authorization |
Example
@PreAuthorize("hasRole('ADMIN')")
Only administrators can execute the method.
Q5. What is @PreAuthorize?
Answer
@PreAuthorize checks authorization before executing a method.
Example
@PreAuthorize("hasRole('ADMIN')")
public void deleteUser(Long id) {
}
Execution Flow
Request
↓
Authentication
↓
Authorization
↓
Execute Method
If authorization fails, the method is never executed.
Q6. What is @PostAuthorize?
Answer
@PostAuthorize evaluates authorization after the method executes.
It is commonly used when authorization depends on the returned object.
Example
@PostAuthorize("returnObject.owner == authentication.name")
Flow
Execute Method
↓
Return Object
↓
Authorization Check
↓
Return Response
Use Case
Only allow users to view their own customer record.
Q7. What is the difference between URL Security and Method-Level Security?
Answer
| URL Security | Method-Level Security |
|---|---|
| Protects URLs | Protects Methods |
| Controller Level | Service Level |
| Limited Granularity | Fine-Grained |
| Easier to Bypass Internally | Protects Business Logic |
| Good for APIs | Good for Enterprise Applications |
Enterprise Recommendation
Use both URL Security and Method-Level Security together.
Q8. What are common Method-Level Security expressions?
Answer
Spring Security supports powerful authorization expressions.
Examples
hasRole('ADMIN')
hasAnyRole('ADMIN','MANAGER')
hasAuthority('READ_ACCOUNT')
authentication.name == #username
isAuthenticated()
These expressions provide flexible authorization without writing custom code.
Q9. What are the best practices for Method-Level Security?
Answer
Follow these best practices:
- Secure service methods instead of only controllers.
- Use
@PreAuthorizewhenever possible. - Apply Least Privilege.
- Keep authorization rules simple.
- Avoid hardcoded usernames.
- Use roles and authorities.
- Log authorization failures.
- Protect administrative operations.
- Combine with JWT authentication.
- Test authorization rules thoroughly.
Production Flow
Client
↓
JWT Authentication
↓
Spring Security
↓
Method Authorization
↓
Business Service
↓
Database
Q10. What is the recommended enterprise architecture for Method-Level Security?
Answer
Enterprise applications implement authorization at multiple layers.
Enterprise Architecture
User
│
HTTPS
│
API Gateway
│
JWT Authentication
│
Spring Security Filter
│
Controller Layer
│
@PreAuthorize Check
│
Business Service
│
Business Validation
│
Database
Layered Security Model
Authentication
│
URL Security
│
Method Security
│
Business Validation
│
Database Constraints
Senior Interview Tip
Method-Level Security should never replace URL Security.
Enterprise applications typically use multiple authorization layers:
- API Gateway Authentication
- URL-Level Security
- Method-Level Security
- Business Validation
- Database Constraints
This layered approach ensures that even if one security layer is bypassed, the business methods remain protected.
Quick Revision
- Method-Level Security protects individual methods.
- Enable it using
@EnableMethodSecurity. @PreAuthorizeexecutes before the method.@PostAuthorizeexecutes after the method.- Secure service methods, not just controllers.
- Combine URL Security with Method-Level Security.
- Use roles and authorities instead of hardcoded values.
- Apply the Principle of Least Privilege.
- Test authorization rules thoroughly.
- Enterprise applications implement layered authorization.