I am using Spring Security for permission checking on methods. I would like to call a private method to collect some data to send to hasPermission()
method. Following is something I am trying to execute and I get SpelEvaluationException because Spring is looking for the localPrivateMethod
in MethodSecurityExpressionRoot
. Is there a way to achieve this? Thanks.
@PreAuthorize("hasPermission(new Object[]{#arg3, #localPrivateMethod(#arg1,#arg2)}, 'canDoThis')")
public long publicMethod1(long arg1, long arg2, long arg3) {}
private String localPrivateMethod(long a1, long a2) {}
Private methods cannot be called, but you can refer to "this component" through
this.
:You will not be able to call a private method, but you will be able to call a method in another spring bean. In my app I have an @Component named permissionEvaluator. I then reference it in a @PreAuthorize like so:
PermissionEvaluatorImpl looks like this:
and PermissionEvaluator is my own interface with nothing special, just whatever methods I need to evaluate.