Complete the code to declare a class that implements PermissionEvaluator.
public class CustomPermissionEvaluator implements [1] { }
The class must implement PermissionEvaluator to define custom permission logic.
Complete the method signature to override the hasPermission method with target domain object.
@Override public boolean hasPermission(Authentication authentication, Object targetDomainObject, [1] permission) { return false; }
The permission parameter is a String describing the permission to check.
Fix the error in the method to check if the user has the required permission string.
public boolean hasPermission(Authentication authentication, Object targetDomainObject, String permission) {
return authentication.getAuthorities().stream()
.anyMatch(auth -> auth.getAuthority().[1](permission));
}Use equals to check exact match of authority string with permission.
Fill both blanks to implement the second hasPermission method that checks permission by target ID and type.
@Override public boolean hasPermission(Authentication authentication, Serializable targetId, [1] targetType, [2] permission) { // Custom logic here return false; }
Both targetType and permission parameters are String types in this method.
Fill all three blanks to create a map of permissions for a user with filtering by permission string.
Map<String, Boolean> permissions = user.getRoles().stream()
.flatMap(role -> role.getPermissions().stream())
.filter(p -> p.getName().[1]([2]))
.collect(Collectors.toMap(
p -> p.getName(),
p -> true
));
boolean hasAccess = permissions.getOrDefault([3], false);Use contains to filter permissions containing "read" and check access for "read" permission.