在 Jersey 2.22.2 中获取客户端 ip

2023-12-26

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

我尝试过

@Context HttpServletRequest

并且还与

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

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

Setup

  • 泽西岛 v:2.22.2
  • 灰熊诉:2.3.22
  • Java v:8

休息.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 测试 引导程序 罐 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()应返回远程 IP 地址的方法。 请注意,代理或 NAT 可能会修改 IP 地址。

EDIT :

解决方案是注入 grizzly 请求:

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Jersey 2.22.2 中获取客户端 ip 的相关文章

随机推荐

  • 是否需要释放 RecyclerView 的 ViewHolder 中的视图绑定以避免内存泄漏?

    在Google提供的ViewBinding示例中 我们需要将Fragment的viewBinding设置为null 但对于Activity则不需要 https developer android com topic libraries vi
  • qt 5.8 sql 连接错误:Windows 10 上未加载 QMYSQL 驱动程序

    当我尝试连接mysql时 出现错误 QSqlDatabase QMYSQL driver not loaded QSqlDatabase available drivers QSQLITE QMYSQL QMYSQL3 QODBC QODB
  • 关闭文本区域大小调整

    我正在尝试关闭网站中的文本区域大小调整功能 现在我正在使用这个方法 textarea clear left min width 267px max width 267px min height 150px max height 150px
  • boost::spirit 中的运算符优先级?

    我使用spirit mini c 样本做了一些测试 不幸的是 它没有按预期保持运算符优先级 int main return 3 gt 10 3 gt 1 评估为 0 return 3 gt 10 3 gt 1 返回 1 我试图移动 的定义和
  • Mouseover / Mouseenter 闪烁与工具提示交互

    鼠标悬停监听父元素 但是当移动到工具提示 子 元素时 它会闪烁 控制台日志显示好像 mouseover mouseenter 事件被快速触发 Mouseenter 根本不保留工具提示显示 element on mouseenter hove
  • 如何在JavaCV中迭代cvMat矩阵?

    我有一个在矩阵中转换的 IplImage 现在我想逐个单元地迭代 CvMat mtx new CvMat iplUltima for int i 0 i lt 100 i I need something like mtx 0 i some
  • 获取对象数组中所有指定元素的总和

    我有一个对象数组 如下所示 width 128 90663423245883 height 160 X 0 Y 140 width 277 0938568683375 height 263 X 128 90663423245883 Y 37
  • 正则表达式查找两个字符串的最长公共前缀

    是否有一个正则表达式可以找到两个字符串的最长公共前缀 如果这不能通过一个正则表达式来解决 那么使用正则表达式 perl ruby python 任何东西 的最优雅的代码或 oneliner 是什么 PS 我可以通过编程轻松地做到这一点 我只
  • 如何更改 ggplotly 中条形图的悬停背景颜色

    我使用 ggplotly 绘制图表 当光标移动到图表顶部时 我使用工具提示函数来表示条形图中的值 Source Data lt data frame key c 1 1 1 2 2 2 3 3 3 Product Name c Table
  • 安装 Xcode 4.2 后无法使用本机扩展构建 json (1.6.3)

    您熟悉这个错误吗 我不知道如何解决这个问题 Installing json 1 6 3 with native extensions Gem Installer ExtensionBuildError ERROR Failed to bui
  • 单页应用程序的 AppEngine app.yaml 配置

    我的 app yaml 文件遇到问题 我在 AppEngine 上有一个带有 python 运行时的单页应用程序 Angular2 应用程序 但深层链接未正确路由 这是我的 app yaml 文件 runtime python27 api
  • 上传前获取视频尺寸,客户端

    我有一个视频上传表单 用户单击 浏览 并选择一个文件 如何在用户选择文件时 在文件上传到服务器之前 获取视频尺寸 显然 它必须是客户端的东西 可能是 javascript jquery 或 flash flex 不过更喜欢 js jquer
  • Android 上的 ZXing PDF417

    有人能读到吗PDF417 http en wikipedia org wiki PDF417条形码与使用ZXing http code google com p zxing Android 操作系统上的库 他们支持这一点 根据他们的页面 它
  • 替换捕获组

    如果我有一个带有捕获组的正则表达式 例如foo f 如果我将其与字符串匹配并想要替换第一个捕获组在所有比赛中 with baz so that foo f blah foo f 转换为 foobaz blah foobaz 使用标准库似乎没
  • 错误:尝试使用 pip 安装 pocketsphinx 时,pocketsphinx 的构建轮失败

    我尝试安装 pocketsphinx 并收到以下错误 jandornhege JanDornhegeUbuntu Hermes Basefunktions pip install pocketsphinx Collecting pocket
  • SwiftUI 以编程方式从可表示返回到视图

    我正在尝试在新的 swift ui 应用程序中设置 qr 阅读器 我可以用这一行加载 UIKit qr 阅读器视图 NavigationLink destination QRCodeScan Text Scan QR 这是我的 UIView
  • Spring MVC 表单输入值始终为 null

    我是 Spring MVC 的新手 但对 Java 的 Web 开发并不陌生 我正在尝试创建一个简单的表单 gt 控制器示例 我有一个表单 一个表单控制器 在下面粘贴的上下文 XML 中配置 和我的模型 一个简单的 bean 无论如何 当我
  • 分析和优化游戏 android

    我正在制作我的第一个 Android 游戏 它将是一个使用 opengl es 的 3D 街机游戏 我已经为此工作了很长一段时间 主要是优化引擎以适应未来的灵活性 无论如何 现在我已经完全完成了游戏功能和所有漂亮的东西 但它在不是我的调试手
  • 8.3 之后无法在 Spotlight 中使用键盘扩展

    自从我将键盘扩展项目更新到 Swift 1 2 并将设备更新到 iOS 8 3 后 我无法再在 Spotlight 搜索中使用我的扩展 如果我按住 地球 键 则我的分机不在列表中 如果进入 Spotlight 时它是活动键盘 则会使用系统键
  • 在 Jersey 2.22.2 中获取客户端 ip

    我正在尝试访问正在调用我的其余服务器的客户端 IP 但我只得到 null 作为响应 网络服务器正在运行 我可以从网络浏览器访问它 我尝试过 Context HttpServletRequest 并且还与 Context ContainerR