[英]JAX-RS 2.0 Client - send multipart message with RESTEasy client
[英]retrieving client IP address in jax-rs resteasy interceptor
我有一个 REST Webservice 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 地址?
ContainerRequestContext
类提供了丰富的 API 来获取特定于请求的信息,例如请求 URI、标头、实体、cookie 或请求范围的属性。 但是,不幸的是,它不提供有关客户端 IP 地址的信息。
要走的路是在您的过滤器中注入HttpServletRequest
:
@Context
HttpServletRequest httpRequest;
然后使用ServletRequest#getRemoteAddr()
提取客户端 IP 地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.