如何在Spring MVC Controller中提取IP地址?

时间:2021-11-14 02:57:06

I am working on Spring MVC controller project in which I am making a GET URL call from the browser -

我正在处理Spring MVC控制器项目,我正在从浏览器中进行GET URL调用 -

Below is the url by which I am making a GET call from the browser -

以下是我通过浏览器进行GET调用的网址 -

http://127.0.0.1:8080/testweb/processing?workflow=test&conf=20140324&dc=all

And below is the code in which the call comes after hitting at the browser -

以下是在浏览器上点击后调用的代码 -

@RequestMapping(value = "processing", method = RequestMethod.GET)
public @ResponseBody ProcessResponse processData(@RequestParam("workflow") final String workflow,
    @RequestParam("conf") final String value, @RequestParam("dc") final String dc) {

        System.out.println(workflow);
        System.out.println(value);
        System.out.println(dc);

        // some other code
    }

Problem Statement:-

问题陈述:-

Now is there any way, I can extract IP Address from some header? Meaning I would like to know from which IP Address, call is coming, meaning whoever is calling above URL, I need to know their IP Address. Is this possible to do?

现在有什么办法,我可以从一些标题中提取IP地址吗?意思是我想知道哪个IP地址,呼叫即将到来,意味着无论谁在上面调用URL,我都需要知道他们的IP地址。这可能吗?

3 个解决方案

#1


84  

The solution is

解决方案是

@RequestMapping(value = "processing", method = RequestMethod.GET)
public @ResponseBody ProcessResponse processData(@RequestParam("workflow") final String workflow,
    @RequestParam("conf") final String value, @RequestParam("dc") final String dc, HttpServletRequest request) {

        System.out.println(workflow);
        System.out.println(value);
        System.out.println(dc);
        System.out.println(request.getRemoteAddr());
        // some other code
    }

Add HttpServletRequest request to your method definition and then use the Servlet API

将HttpServletRequest请求添加到方法定义,然后使用Servlet API

Spring Documentation here said in

Spring文档在这里说

15.3.2.3 Supported handler method arguments and return types

15.3.2.3支持的处理程序方法参数和返回类型

Handler methods that are annotated with @RequestMapping can have very flexible signatures.
Most of them can be used in arbitrary order (see below for more details).

Request or response objects (Servlet API). Choose any specific request or response type,
for example ServletRequest or HttpServletRequest

#2


71  

I am late here, but this might help someone looking for the answer. Typically servletRequest.getRemoteAddr() works.

我来晚了,但这可能有助于寻找答案的人。通常servletRequest.getRemoteAddr()有效。

In many cases your application users might be accessing your web server via a proxy server or maybe your application is behind a load balancer.

在许多情况下,您的应用程序用户可能通过代理服务器访问您的Web服务器,或者您的应用程序可能位于负载均衡器之后。

So you should access the X-Forwarded-For http header in such a case to get the user's IP address.

因此,在这种情况下,您应该访问X-Forwarded-For http标头以获取用户的IP地址。

e.g. String ipAddress = request.getHeader("X-FORWARDED-FOR");

例如String ipAddress = request.getHeader(“X-FORWARDED-FOR”);

Hope this helps.

希望这可以帮助。

#3


3  

You can get the IP address statically from the RequestContextHolder as below :

您可以从RequestContextHolder中静态获取IP地址,如下所示:

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
        .getRequest();

String ip = request.getRemoteAddr();

#1


84  

The solution is

解决方案是

@RequestMapping(value = "processing", method = RequestMethod.GET)
public @ResponseBody ProcessResponse processData(@RequestParam("workflow") final String workflow,
    @RequestParam("conf") final String value, @RequestParam("dc") final String dc, HttpServletRequest request) {

        System.out.println(workflow);
        System.out.println(value);
        System.out.println(dc);
        System.out.println(request.getRemoteAddr());
        // some other code
    }

Add HttpServletRequest request to your method definition and then use the Servlet API

将HttpServletRequest请求添加到方法定义,然后使用Servlet API

Spring Documentation here said in

Spring文档在这里说

15.3.2.3 Supported handler method arguments and return types

15.3.2.3支持的处理程序方法参数和返回类型

Handler methods that are annotated with @RequestMapping can have very flexible signatures.
Most of them can be used in arbitrary order (see below for more details).

Request or response objects (Servlet API). Choose any specific request or response type,
for example ServletRequest or HttpServletRequest

#2


71  

I am late here, but this might help someone looking for the answer. Typically servletRequest.getRemoteAddr() works.

我来晚了,但这可能有助于寻找答案的人。通常servletRequest.getRemoteAddr()有效。

In many cases your application users might be accessing your web server via a proxy server or maybe your application is behind a load balancer.

在许多情况下,您的应用程序用户可能通过代理服务器访问您的Web服务器,或者您的应用程序可能位于负载均衡器之后。

So you should access the X-Forwarded-For http header in such a case to get the user's IP address.

因此,在这种情况下,您应该访问X-Forwarded-For http标头以获取用户的IP地址。

e.g. String ipAddress = request.getHeader("X-FORWARDED-FOR");

例如String ipAddress = request.getHeader(“X-FORWARDED-FOR”);

Hope this helps.

希望这可以帮助。

#3


3  

You can get the IP address statically from the RequestContextHolder as below :

您可以从RequestContextHolder中静态获取IP地址,如下所示:

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
        .getRequest();

String ip = request.getRemoteAddr();