I would like to secure my services layer using Spring Security. As explained in the documentation, I need to use a MethodSecurityInterceptor that will check if the method invocation is allowed.
To decide if a service method invocation is allowed for a given user, affecting a required role to the invoked method (using MethodSecurityMetadataSource) is not enough for me since it also depends on the parameters passed to the method. As suggested in the documentation, I can write a custom AccessDecisionVoter and access the arguments though the secured object (MethodInvocation in this case).
But, my authorization logic is different across the methods. For example, the arguments may be different between multiple methods and the authorization logic will also be different.
I see two options:
- I can use conditional logic in the
AccessDecisionVoterto determine the invoked method and the authorization logic to use, but it seems to be an ugly solution. - I can define one
MethodSecurityInterceptorper method to secure. According to the Spring documentation, aMethodSecurityInterceptoris used to secure many methods, so it makes me thinking there is another way.
The same question exists for access decision after method invocation (using AfterInvocationProvider).
What are the alternatives?
You can implement your own method security annotations based on Spring
@PreAuthorize("")construction.To fetch extra information about the method(beyond method argument values) to SpEL evaluation context you can implement your own MethodSecurityExpressionHandler
and register it in your
global-method-securitydeclarationNow you can create custom security annotations(and extra process annotation data in MySecurityExpressionHandler if required)
for example you can create a custom annotation to check user roles without messing with strings: