Access to request source IP in Spring 3 MVC web application

蹲街弑〆低调 提交于 2019-12-04 03:42:11

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!