4

I would like to have one of my pointcut being triggered according to how I name a path variable in my URL.

The fact is that it is a String and I don't want my pointcut to be triggered on every String.

@Pointcut("@annotation(security.annotation.RequireValidUser) && args(datasetName, ..)")
private void methodAnnotatedForValidDatasetName(String datasetName) {
}

In my case this pointcut is successfully triggered in this method (which is ok):

public ResponseEntity<ApiKeyDTO> createApiKey(@PathVariable("name") String datasetName, @RequestBody ApiKeyDTO apiKeyDTO)

But is is also triggered here (which I don't want):

public ResponseEntity<List<ApiKeyDTO>> findApiKeyFromDatasetLabel(@PathVariable("label") String datasetLabel)

I wonder if there are any workaround for me so the pointcut is able to distinguish different Strings with their naming maybe?

EDIT: As suggested by @kuhajeyan, I tried to use 'argNames' as following:

@Pointcut(value = "@annotation(security.annotation.RequireValidUser) && args(datasetName, ..)", argNames = "datasetName")

Unfortunately it doesn't work as intented, it only specifies the name of the arguments inside the pointcut.

Rlarroque
  • 1,900
  • 1
  • 17
  • 27
  • 1
    You do not want to match on parameter names - **bad idea!** - as they are not part of an API contract and purely a source code thing usually unavailable in the byte code and often subject to change. The contract is the method signature. So you want to differentiate your pointcut based on the signature, such as method return types, parameter number, types and order, method annotations, parameter annotations. – kriegaex Sep 03 '16 at 11:06
  • I think you made a good point. I would need to re-think the way I designed my endpoints. Thank you. – Rlarroque Sep 07 '16 at 12:54

1 Answers1

3

you can use argName , Documentated here

argNames

@Pointcut(value="@annotation(security.annotation.RequireValidUser) && args(name,..)",  argNames="datasetName")
private void methodAnnotatedForValidDatasetName(String datasetName) {
}
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • Well thank, I think I missed it while browsing this documentation, my bad. – Rlarroque Sep 02 '16 at 12:08
  • Alright, I tested with argNames just like that: `@Pointcut(value = "@annotation(security.annotation.RequireValidUser) && args(datasetName, ..)", argNames = "datasetName")` But unfortunately it is not working as intented. argNames is used to explicitly specify the names of the arguments of a pointcut but not to filter the arguments of a method that triggers the pointcut. – Rlarroque Sep 02 '16 at 13:06
  • @Rlarroque you are correct. I have tested it. Not sure if this still possible once compiled not sure if argument name is preserved. for example you can always override method in interface with the argument name changed – kuhajeyan Sep 02 '16 at 15:06