Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to request source IP in Spring 3 MVC web application

Is there any way in Spring 3 MVC to gain access to the request header information (like source IP address etc.), when parsing a request in a @Controller?

like image 738
Nico Huysamen Avatar asked Jan 19 '12 12:01

Nico Huysamen


1 Answers

You can retrieve it from HttpServletRequest, using getRemoteAddr() to get access to user IP address and getHeader() to get header value.

For example

@Controller
public class MyController {
    @RequestMapping(value="/do-something")
    public void doSomething(HttpServletRequest request) {
        final String userIpAddress = request.getRemoteAddr();
        final String userAgent = request.getHeader("user-agent");
        ....
    }
}

You may pass other parameters to the doSomething() method, like model or request params.

like image 196
ruslan Avatar answered Oct 22 '22 12:10

ruslan