WICKET: how to get client's ip/address
Asked Answered
C

3

6

I'm using wicket 1.5.1, couldn't figure this out.

public class MyPage extends WebPage {

public MyPage() {

    String clientAddress = ...?
Crossopterygian answered 14/10, 2011 at 14:33 Comment(0)
G
15
    WebRequest req = (WebRequest) RequestCycle.get().getRequest();
    HttpServletRequest httpReq = (HttpServletRequest) req.getContainerRequest();
    String clientAddress = httpReq.getRemoteHost();
Gilmour answered 14/10, 2011 at 19:46 Comment(2)
Shouldn't that be httpReq.getRemoteAddr()?Sandarac
getRemoteAddr() will return the raw IP address, while getRemoteHost() will return the resolved address name if possible (or the IP if not).Gilmour
R
3

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.

Rex answered 16/10, 2011 at 11:2 Comment(2)
How do I get an instance of WebClientInfo in Wicket 6?Businessman
@Artem Look at the code in org.apache.wicket.markup.html.pages.BrowserInfoPage for detailed informationsBloody
V
2

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

String remoteAddress = ((WebClientInfo)Session.get().getClientInfo())
                      .getProperties()
                      .getRemoteAddress();
Veolaver answered 11/9, 2015 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.