简体   繁体   中英

WICKET: how to get client's ip/address

I'm using wicket 1.5.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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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