【发布时间】:2014-07-04 12:57:03
【问题描述】:
我有一个 REST Web 服务 API,我需要通过几个标准来保护它。这是我的拦截器的剥离示例:
@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
}
}
如果是 PermitAll,我需要添加 IP-Check。在这个地方如何获取来电IP地址?
【问题讨论】:
-
注入
HttpServletRequest,获取IP地址like explained here。
标签: java web-services rest jax-rs resteasy