0

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?

Alex Tbk
  • 2,042
  • 2
  • 20
  • 38
  • What are you looking for? @MyNewAnnotation(abc = #locationDTO.parent) or @MyNewAnnotation("myNewFunction(#locationDTO.parent)") – Glen Mazza Sep 07 '20 at 18:54
  • I am looking for @MyNewAnnotation(permission = "SOMETHING", target = "locationDTO.parent") – Alex Tbk Sep 07 '20 at 19:24
  • Does my answer about [how to evaluate SpEL (Spring Expression Language)](https://stackoverflow.com/a/53825701/1082681) help? – kriegaex Sep 08 '20 at 00:59

1 Answers1

0

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.

Alex Tbk
  • 2,042
  • 2
  • 20
  • 38