Spring preAuthorize log result of the SpEL query?

366 Views Asked by At

I have a method that can be executed when either of the authentication methods return true.

@PreAuthorize("canExec('ROLE_A') || canExec('ROLE_B')")
public String getSomething() {
    return "Something";
}

How can I log whether the authentication failed or succeeded, meaning the result of the whole SpEL query is true or false?

The following is not a possible solution, since it can be called multiple times in the same SpEL, and the multiple logged results would not reflect the actual result of the authorization.

public boolean canExec(String role) {
    boolean result = ...acutal evaluation...;
    log.info("auth result for role {}: {}", role, result);
    return result;
}
1

There are 1 best solutions below

1
Gary Russell On
public boolean canExecOr(String roleA, String RoleB) {
    boolean canA = canExec(roleA);
    boolean canB = canExec(roleB);
    // log...
   return canA || canB;
}

@PreAuthorize("canExecComposite('ROLE_A','ROLE_B')")

??