12

My use case:
I will have a lot of @PreAuthorize annotation in the form @PreAuthorize("hasAuthority('RESPECT_MY_AUTHORITY')").
I'd like to create a meta-annotation @HasAuthority which takes the authority as a value and pass it to @PreAuthorize("hasAuthority(<value>)").

It feels like it's not possible. The closest I get to what I want is something like the @AliasFor annotation. but the problem is that I can't add anything to the value I'll get in @HasAuthority. So I would have to repeat the hasAuthority part everytime.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasAuthority('RESPECT_MY_AUTHORITY')")
public @interface HasAuthority {

    @AliasFor(annotation = PreAuthorize.class, attribute = "value")
    String value();

}

I'd like something like:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasAuthority(#value)")
public @interface HasAuthority {

    String value();

}

Any idea how I could do that, or if it's possible at all?

grandouassou
  • 2,500
  • 3
  • 25
  • 60
  • have you found any solution? – UKoehler Oct 14 '21 at 15:19
  • @UKoehler nope, not really. For now I just put the whole `hasAuthority('RESPECT_MY_AUTHORITY')` in a _constants_ file so that I can statically import those strings and use `@PreAuthorize(RESPECT_MY_AUTHORITY)`. – grandouassou Dec 23 '21 at 23:11

0 Answers0