在 Jersey 2.22.2 中获取客户端 ip

Get client ip in Jersey 2.22.2

我正在尝试访问调用我的休息服务器的客户端 IP,但我只得到 null 作为响应。 Web 服务器是 运行,我可以从 Web 浏览器访问它。

我试过

@Context HttpServletRequest

还有

@Context ContainerRequest request
request.getRequestHeader("HTTP_FORWARDED")
//HTTP_X_FORWARDED
//HTTP_CLIENT_IP

但都没有成功,响应为 null 或空白。

设置

Rest.java

   import javax.ws.rs.GET;
   import javax.ws.rs.Path;
   import javax.ws.rs.PathParam;
   import javax.ws.rs.Produces;
   import javax.ws.rs.QueryParam;
   import javax.ws.rs.container.ContainerRequestContext;
   import javax.ws.rs.core.Context;
   import javax.ws.rs.core.MediaType;
   import javax.ws.rs.core.Request;
   import javax.ws.rs.core.UriInfo; 

   @Path("/rest")
   public class Rest {
   @GET
   @Path("/test/")
   @Produces(MediaType.APPLICATION_JSON)
   public TestAddress test(@Context HttpServletRequest re){
      System.out.println(re.getRemoteAddr());
      TestAddress adr = new TestAddress();
      adr.setAge(32);
      adr.setName("Fidde");
      adr.setSurename("Lass");

      //System.out.println(uriInfo.getBaseUri());
      return adr;
   }

main.java

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import java.io.IOException;
import java.net.URI;

public class Main {
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:8080/myapp/";

/**
 * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
 * @return Grizzly HTTP server.
 */
public static HttpServer startServer() {
    // create a resource config that scans for JAX-RS resources and providers
    // in com.example package
    final ResourceConfig rc = new ResourceConfig().packages("com.example");

    // create and start a new instance of grizzly http server
    // exposing the Jersey application at BASE_URI
    return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}

/**
 * Main method.
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
    final HttpServer server = startServer();
    System.out.println(String.format("Jersey app started with WADL available at "
            + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
    System.in.read();
    server.stop();
}

}

Pom.xml
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 测试 Bootstrap 罐 0.0.1-快照

<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>backend.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.22.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.2.3</version>
        <scope>compile</scope>
    </dependency>
</dependencies>


<properties>
    <jersey.version>2.22.2</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

HttpServletRequest 提供了一个 getRemoteAddr() 方法,应该 returns 远程 IP 地址。 请注意,代理或 NATing 可能会修改 IP 地址。

编辑:

解决方案是注入灰熊请求:

@GET
@Path("/test/")
@Produces(MediaType.APPLICATION_JSON)
public TestAddress test(@Context org.glassfish.grizzly.http.server.Request re) {
    System.out.println(re.getRemoteAddr());
    ...
}

这对我有用,但它完全依赖灰熊。