If you are using JAX-RS 2.0, you can enter ResourceInfo in ContainerRequestFilter , then get java.lang.reflect.Method from. From Method you can get annotation. for instance
@Provider @Priority(Priorities.AUTHENTICATION) public class SecurityFilter implements ContainerRequestFilter { @Context private ResourceInfo resourceInfo;
This ( ResourceInfo ) is necessary, but if you need to get some value from the annotation, for example @SecurityCheck("SomeRoleAllowed") .
If you don't need a value, and all you want is for any method annotated for filtering, then you can either create a DynamicFeature where you bind each method to a filter. for instance
@Provider public class SecurityCheckDynamicFeature implements DynamicFeature { @Override public void configure(ResourceInfo info, FeatureContext context) { Method method = info.getResourceMethod(); SecurityCheck annotation = method.getAnnotation(SecurityCheck.class); if (annotation != null) { context.register(SecurityFilter.class); } } }
Or another way is to simply use @NameBinding in a custom annotation
@NameBinding @Target(...) @Retention public @interface SecurityCheck {}
Then you also need to annotate the SecurityFilter class with annotation. Any method or class annotated will go through the filter.
Other resources:
source share