1

How can I get the remote IP Address? -- SOLVED

asked 2024-01-31 06:51:40 +0800

dormitionskete gravatar image dormitionskete
15 4

updated 2024-03-12 10:22:10 +0800

How can I get the remote IP Address?

"Executions.getCurrent().getRemoteAddr()" used to work, but now only returns 127.0.0.1

Somebody on this list said they solved it using the header X-Forwarded-For.

But how?

Any help would be greatly appreciated.

======================================================================

It didn't seem to want to take my answer, but we'll try it this way...


Well, this has been very helpful, and interesting. I think I've finally gotten to the bottom of this.

Up until late February 2019, since we ran both Apache and Tomcat on the same server, we used to specify the port numbers for Tomcat in the URL. In late February 2019, we added the following to our Apache config to hide the port numbers.

  RewriteEngine on
  ProxyPass / https://www.example.com:8443/
  ProxyPassReverse / https://www.example.com:8443/

In looking into this further, it appears that that is when our ZK applications stopped capturing the IP Address, and would only return 127.0.0.1.

Given the following code:

Session sess = Sessions.getCurrent();
String remoteIPAddress = Executions.getCurrent().getRemoteAddr();
dsLabel1.setValue(remoteIPAddress);

//--
HttpSession nativeSession = (HttpSession) Sessions.getCurrent().getNativeSession();
String nativeSessionIP = Executions.getCurrent().getRemoteAddr();
dsLabel2.setValue(nativeSessionIP);

// --
String forwaredForHeader = ((HttpServletRequest) Executions.getCurrent().getNativeRequest()).getHeader("X-Forwarded-For");
dsLabel3.setValue("---" + forwaredForHeader + "---");

The following (modified) URL's returned this:

http://example.com:8081/ZKTestIPAddress/

216.123.125.152
216.123.125.152
---null---

https://secure.example.com:8443/ZKTestIPAddress/

216.123.125.152
216.123.125.152
---null---

https://www.example.net/ZKTestIPAddress/

127.0.0.1
127.0.0.1
---216.123.125.152---

So it looks like this is not a problem with ZK. It is entirely a problem with our environment. But thanks to your help, we should be able to get it to work again!

Many, MANY THANKS !!!

Much appreciated.

delete flag offensive retag edit

5 Answers

Sort by ยป oldest newest most voted
1

answered 2024-03-05 14:25:18 +0800

roberrtt gravatar image roberrtt
11 2

I too wanna know the same thing can anyone spare a time to explain this.

link publish delete flag offensive edit
1

answered 2024-03-11 15:07:26 +0800

MDuchemin gravatar image MDuchemin
2560 1 6
ZK Team

In that case, if your server is doing an internal forward and stores the previous origin IP in the x-forwarded-for header, you can check the content of that header by accessing the native request:

String forwaredForHeader = ((HttpServletRequest)Executions.getCurrent().getNativeRequest()).getHeader("X-Forwarded-For");

Note: you will need servlet API (either javax servlet API, or Jakarta servlet API) as a scoped "provided" depedency to have access to the HttpServletRequest interface.

If the value is not in the x-forward-for header, then you may need to check your underlying web server valves / filter to see if anything is removing that info, or if it's not available to begin with.

In both case, ZK doesn't affect this. This is purely with your container (tomcat, jetty, etc.)

link publish delete flag offensive edit
1

answered 2024-03-11 15:09:45 +0800

hawk gravatar image hawk
3250 1 5
http://hawkphoenix.blogsp... ZK Team

call HttpServletRequest.getRemoteAddr() to see whether it's java ee API's issue. ZK Execution is just a wrapper of HttpServletRequest.

See https://www.zkoss.org/wiki/ZKDeveloper%27sReference/Integration/AccessingJavaEEScopeObjects#get_HttpServletRequest

link publish delete flag offensive edit
0

answered 2024-03-11 12:31:00 +0800

MDuchemin gravatar image MDuchemin
2560 1 6
ZK Team

Are you connecting from your local machine? Or are you receiving that request from a different computer?

If that's from your local machine, then that's the correct address. 127.0.0.1 is the loopback address, which is to say the IP equivalent of Localhost. It always point to your local computer.

link publish delete flag offensive edit
0

answered 2024-03-11 13:00:01 +0800

dormitionskete gravatar image dormitionskete
15 4

No, we're not connecting from our local machine. No matter where a person connects from, all we ever get anymore is 127.0.0.1. It used to give us the remote address, but now never does.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
4 followers

RSS

Stats

Asked: 2024-01-31 06:51:40 +0800

Seen: 21 times

Last updated: Mar 12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More