Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a Custom Permission Evaluator in Spring Security?
A Custom Permission Evaluator is a class that implements Spring Security's PermissionEvaluator interface to define fine-grained access control logic beyond simple role checks.
Click to reveal answer
beginner
Which interface must you implement to create a Custom Permission Evaluator?
You must implement the PermissionEvaluator interface, which requires defining the methods hasPermission(Authentication, Object, Object) and hasPermission(Authentication, Serializable, String, Object).
Click to reveal answer
intermediate
How does Spring Security use a Custom Permission Evaluator in method security?
Spring Security calls the hasPermission methods of your Custom Permission Evaluator when you use expressions like @PreAuthorize("hasPermission(...)") to decide if access should be granted.
Click to reveal answer
intermediate
What are the two main methods you must override in a Custom Permission Evaluator?
The two methods are: hasPermission(Authentication authentication, Object targetDomainObject, Object permission) and hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission).
Click to reveal answer
intermediate
Why would you use a Custom Permission Evaluator instead of simple role-based checks?
Because it allows you to check permissions based on the actual domain object or context, enabling more precise and flexible security rules than just checking user roles.
Click to reveal answer
Which interface do you implement to create a Custom Permission Evaluator in Spring Security?
AAuthenticationProvider
BUserDetailsService
CAccessDecisionVoter
DPermissionEvaluator
✗ Incorrect
The PermissionEvaluator interface defines methods for custom permission checks.
What annotation commonly uses hasPermission expressions to invoke a Custom Permission Evaluator?
A@PreAuthorize
B@Controller
C@Service
D@Entity
✗ Incorrect
@PreAuthorize allows method-level security using expressions like hasPermission.
Which method signature is NOT part of the PermissionEvaluator interface?
PermissionEvaluator has two methods: one with targetDomainObject and one with targetId and targetType.
Step 2: Identify the method for domain object permission check
The method hasPermission(Authentication authentication, Object targetDomainObject, Object permission) is used to check permissions on a domain object.
Final Answer:
hasPermission(Authentication authentication, Object targetDomainObject, Object permission) -> Option B
Quick Check:
Domain object permission method = hasPermission with targetDomainObject [OK]
Hint: Override hasPermission with targetDomainObject for object checks [OK]
Common Mistakes:
Choosing methods not in PermissionEvaluator interface
Confusing method parameters
Using method names that don't exist
3. Given this custom PermissionEvaluator method snippet:
public boolean hasPermission(Authentication auth, Object target, Object perm) {
if (auth == null || target == null || !(perm instanceof String)) {
return false;
}
String permission = (String) perm;
User user = (User) auth.getPrincipal();
return user.getRoles().contains(permission);
}
What will be the result if auth is null?
medium
A. Returns false immediately
B. Throws NullPointerException
C. Returns true by default
D. Ignores null and continues
Solution
Step 1: Analyze the null check at method start
The method checks if auth is null and returns false immediately if so.
Step 2: Understand the flow when auth is null
Since auth == null triggers return false, no further code runs and no exception occurs.
Final Answer:
Returns false immediately -> Option A
Quick Check:
Null auth returns false immediately [OK]
Hint: Null checks return false early to avoid exceptions [OK]
Common Mistakes:
Assuming NullPointerException will be thrown
Thinking it returns true by default
Ignoring the null check logic
4. You wrote this custom PermissionEvaluator method:
public boolean hasPermission(Authentication auth, Object target, Object perm) {
String permission = (String) perm;
User user = (User) auth.getPrincipal();
return user.getRoles().contains(permission);
}
What is the main problem with this code?
medium
A. It should return true by default
B. Casting perm to String is unnecessary
C. User roles cannot be checked this way
D. It lacks null checks and may throw NullPointerException
Solution
Step 1: Check for missing null validations
The method does not check if auth, perm, or auth.getPrincipal() are null before casting or calling methods.
Step 2: Understand consequences of missing null checks
If any are null, the code will throw NullPointerException at runtime.
Final Answer:
It lacks null checks and may throw NullPointerException -> Option D
Quick Check:
Missing null checks cause runtime exceptions [OK]
Hint: Always add null checks before casting or method calls [OK]
Common Mistakes:
Ignoring null safety
Thinking casting is always safe
Assuming roles check is invalid
5. You want to create a custom PermissionEvaluator that allows a user to edit a document only if they have the "EDITOR" role and the document status is "DRAFT". Which code snippet correctly implements this logic inside hasPermission?
hard
A. if (auth == null || target == null) return false;
User user = (User) auth.getPrincipal();
Document doc = (Document) target;
return user.getRoles().contains("EDITOR") && "DRAFT".equals(doc.getStatus());
B. User user = (User) auth.getPrincipal();
Document doc = (Document) target;
return user.getRoles().contains("EDITOR") || doc.getStatus().equals("DRAFT");
C. if (auth == null) return true;
Document doc = (Document) target;
return doc.getStatus() == "DRAFT";
D. User user = (User) auth.getPrincipal();
return user.getRoles().contains("EDITOR");
Solution
Step 1: Check for null authentication and target
Security checks should return false if authentication or target is null to avoid errors.
Step 2: Verify user role and document status conditions
The user must have "EDITOR" role and the document status must be exactly "DRAFT" for permission to be granted.
Step 3: Confirm correct logical operator usage
Both conditions must be true, so use logical AND (&&), not OR (||).
Final Answer:
if (auth == null || target == null) return false;
User user = (User) auth.getPrincipal();
Document doc = (Document) target;
return user.getRoles().contains("EDITOR") && "DRAFT".equals(doc.getStatus()); -> Option A
Quick Check:
Check nulls + role AND status = correct logic [OK]
Hint: Use && to combine role and status checks with null safety [OK]