With Jersey 2 can use ContainerRequestFilter
.
@Provider
public class CheckPermissionsRequestFilter
implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext crc) throws IOException {
}
}
We can get the annotation on the called method through the ResourceInfo
class
@Context
private ResourceInfo info;
@Override
public void filter(ContainerRequestContext crc) throws IOException {
Method method = info.getResourceMethod();
CheckPermissions annotation = method.getAnnotation(CheckPermissions.class);
if (annotation != null) {
String[] permissions = annotation.value();
}
}
You can use this annotation
@NameBinding
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckPermissions {
String[] value();
}
And annotate the resource class or the resource method with @CheckPermissions({...})
UPDATE
The annotation above allows for annotating classes also. Just for completeness, you'll want to check the class also. Something like
Class resourceClass = info.getResourceClass();
CheckPermissions checkPermissions = resourceClass.getAnnotation(CheckPermissions.class);
if (checkPermissions != null) {
String[] persmission = checkPermissions.value();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…