在Jersey 2.22.2中获取客户端ip

编程入门 行业动态 更新时间:2024-10-15 12:29:18
本文介绍了在Jersey 2.22.2中获取客户端ip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试访问正在调用我的休息服务器的客户端ip,但是我只得到null作为响应.网络服务器正在运行,我可以通过网络浏览器进行访问.

I am trying to acess the clients ip that are calling my rest server but I only get null as a respons. The webserver is running and I can access is from the web browser.

我尝试过

@Context HttpServletRequest

@Context HttpServletRequest

还有

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

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

但没有成功,响应为空或为空.

But neither whit sucess, the response is null or blank.

设置

  • Jersey v:2.22.2
  • Grizzly 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.URI; public class Main { // Base URI the Grizzly HTTP server will listen on public static final String BASE_URI = "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 maven.apache/xsd/maven-4.0.0.xsd> 4.0.0 测试 引导程序 罐 0.0.1-SNAPSHOT

Pom.xml maven.apache/xsd/maven-4.0.0.xsd"> 4.0.0 Test Bootstrap 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>

推荐答案

HttpServletRequest提供了一种getRemoteAddr()方法,该方法应返回远程IP地址. 请注意,代理或NAT可能会修改IP地址.

HttpServletRequest provides a getRemoteAddr() method that should returns the remote IP address. Note that proxying or NATing may modify the IP address.

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

The solution is to inject a grizzly request :

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

这对我有用,但是完全依赖于灰熊.

This is working for me, but it is totally grizzly dependant.

更多推荐

在Jersey 2.22.2中获取客户端ip

本文发布于:2023-11-25 23:42:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631812.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   Jersey   ip

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!