I have a REST Webservice API which I need to secure by several criterias. Here is a stripped example of my interceptor:
@Provider
@ServerInterceptor
public class MySecurityInterceptor implements ContainerRequestFilter {
private static final ServerResponse ACCESS_FORBIDDEN = new ServerResponse( "Nobody can access this resource", 403, new Headers<Object>() );;
private static final ServerResponse SERVER_ERROR = new ServerResponse( "INTERNAL SERVER ERROR", 500, new Headers<Object>() );;
@Override
public void filter( ContainerRequestContext requestContext ) throws IOException {
ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker)requestContext.getProperty( "org.jboss.resteasy.core.ResourceMethodInvoker" );
Method method = methodInvoker.getMethod();
if ( !method.getDeclaringClass().isAnnotationPresent( ApiKey.class ) ) {
requestContext.abortWith( SERVER_ERROR );
RuntimeException e = new RuntimeException("...");
throw e;
}
if ( method.isAnnotationPresent( PermitAll.class ) ) { //Everyone can call method
return;
}
// -- No one
if ( method.isAnnotationPresent( DenyAll.class ) ) {
requestContext.abortWith( ACCESS_FORBIDDEN );
return;
}
//... And so on
}
}
In case of PermitAll I need to add an IP-Check. How can I obtain the caller IP adress at this place?