由于现在无法从Grizzly 上下文访问远程 IP 信息,因此我实现了这个肮脏的 hack:
首先,我在我的应用程序中添加了新的依赖注入绑定器,它将 java.net.SocketAddress 实例绑定到包含请求源 IP 的实例:
package test;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import javax.inject.Inject;
import javax.inject.Provider;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.internal.PropertiesDelegate;
import org.glassfish.jersey.process.internal.RequestScoped;
import org.glassfish.jersey.server.ContainerRequest;
public class RemoteAddrBinder extends AbstractBinder {
private static class RemoteAddrProviderFactory implements Factory<SocketAddress> {
@Inject
private Provider<ContainerRequest> request;
@Override
public SocketAddress provide() {
ContainerRequest containerRequest = request.get();
PropertiesDelegate delegate = containerRequest.getPropertiesDelegate();
try {
Field requestField = delegate.getClass().getDeclaredField("request");
requestField.setAccessible(true);
Request grizzlyRequest = (Request) requestField.get(delegate);
return new InetSocketAddress(grizzlyRequest.getRemoteAddr(), grizzlyRequest.getRemotePort());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public void dispose(SocketAddress instance) {
}
}
@Override
protected void configure() { bindFactory(RemoteAddrProviderFactory.class).to(SocketAddress.class).in(RequestScoped.class);
}
}
然后,我在应用程序中注册了这个活页夹:
application.register(new RemoteAddrBinder());
现在,可以将 SocketAddress 注入到任何 JAX-RS 方法中:
@GET
@Path("test")
public Response test(@Context SocketAddress remoteAddr) {
return Response.ok("Your IP is: " + ((InetSocketAddress) remoteAddr).getAddress().getHostAddress()).build();
}
希望这可以帮助某人:)