本文介绍了如何在Spring MVC Controller中提取IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

下面是我正在进行GET的网址从浏览器调用 -

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

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
    }

问题陈述: -

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

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?

推荐答案

解决方案是

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

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

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

Spring Documentation 中说

Spring Documentation here said in



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

这篇关于如何在Spring MVC Controller中提取IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 09:29
查看更多