Search code examples
spring-bootspring-securityaop

Access to method signature variables in annotation


How is it possible to access variables from method signature?

In spring security there is the @PreAuthorize annotation that can make use of hasPermission and access variables passed to the method with #locationDTO

@PreAuthorize("hasPermission(#locationDTO.parent, 'Location', 'LOCATION_CREATE') ")
public ResponseEntity createLocation(@RequestBody Location locationDTO) {
     .....
}

I would like to create a custom annotation that has access to the variables in the same way - how is that possible?


Solution

  • Found out how:

    MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
    ParameterNameDiscoverer parameterNameDiscoverer = new 
    DefaultSecurityParameterNameDiscoverer();
    String[] parameterNames = parameterNameDiscoverer.getParameterNames(methodSignature.getMethod());
    

    returns the list of parameter names, which can be access then.