简体   繁体   English

如何在struts2动作方法中获取客户端IP地址?

[英]How to get the client IP address inside struts2 action method?

Is there a way to get the client's IP address inside an struts2 action method? 有没有办法在struts2动作方法中获取客户端的IP地址?

I have implemented both SesionAware and RequestAware interfaces, but these two inject only the session and request maps. 我已经实现了SesionAwareRequestAware接口,但是这两个接口只注入会话和请求映射。

你试过ServletRequestAware吗?

Try request.getRemoteAddress(). 尝试request.getRemoteAddress()。 This will give you the client IP. 这将为您提供客户端IP。 If the request is coming through a proxy server, then you will get only the ip of the proxy server. 如果请求是通过代理服务器发送的,那么您将只获得代理服务器的IP。

You can try ServletRequest.getRemoteAddress() API call to get client or last proxy's IP address. 您可以尝试ServletRequest.getRemoteAddress()API调用以获取客户端或最后一个代理的IP地址。 This is part of Servlet API and not Struts2 specific. 这是Servlet API的一部分,而不是Struts2的特定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM