2016-05-11 16 views
1

私はクライアントのIPアドレスを取得しようとしています。 Webサーバーが実行されており、Webブラウザからアクセスできます。Jersey 2.22.2でクライアントIPを取得

私は

@Context ContainerRequest要求
request.getRequestHeader( "HTTP_FORWARDED")とも

@ContextのHttpServletRequest

として試してみました
// HTTP_X_FORWARDED
// HTTP_CLIENT_IP

しかし、どちらの聖霊降臨祭の成功事例のは、応答が NULLまたは空白です。

セットアップ

  • ジャージーV:2.22.2
  • グリズリーV:2.3.22
  • のJava V:8

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 テスト ブートストラップ jar 0.0.1-SNAPSHOT

<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> 

答えて

3

HttpServletRequestリモートIPアドレスを返すべきgetRemoteAddr()方法を提供します。 プロキシまたはNATingがIPアドレスを変更する可能性があることに注意してください。

EDIT:

ソリューションは、グリズリーリクエストを注入することである。

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

これは私のために働いているが、それは完全にグリズリー依存しています。

+0

私は 'getRemoteAddr()'にアクセスできますが、nullを返します。上記の例では、正しくアクセスしていますか? – Fredrik

+0

これで動作します。明確な例をありがとう! – Fredrik

関連する問題