开发者

WICKET: how to get client's ip/address

开发者 https://www.devze.com 2023-04-13 05:36 出处:网络
I\'m using wicket 1.5开发者_JAVA技巧.1, couldn\'t figure this out. public class MyPage extends WebPage {

I'm using wicket 1.5开发者_JAVA技巧.1, couldn't figure this out.

public class MyPage extends WebPage {

public MyPage() {

    String clientAddress = ...?


    WebRequest req = (WebRequest) RequestCycle.get().getRequest();
    HttpServletRequest httpReq = (HttpServletRequest) req.getContainerRequest();
    String clientAddress = httpReq.getRemoteHost();


Subclass WebClientInfo to provide a public method that delegates on protected WebClientInfo.getRemoteAddr(). Then create a method to query this in a custom RequestCycle class. In Wicket 1.3-1.4 I've achieved this by subclassing RequestCycle, but with 1.5 it seems things are different: RequestCycle in Wicket 1.5

WebClientInfo has the advantage of querying the X-Forwarded-For erquest parameter, and will return the proper IP address if your server is behind a proxy/load balancer that uses XFF.


Using Wicket 6 and 7, you can do the following:

String remoteAddress = ((WebClientInfo)Session.get().getClientInfo())
                      .getProperties()
                      .getRemoteAddress();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号