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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.