简体   繁体   中英

In Java, how can a website find my real IP address while I'm behind a proxy?

My program will use (http) proxy to send out http requests. And the proxy address is known to the program. But the program also want to get the current IP address while behind proxy. What is the best way to retrieve that?

The only way you could do that would to have your program explicitly send its IP address to the server encoded in the request URL, a custom request header, or in the request body.

If the proxy is under your control, you could configure it to add your client's IP address to the request. (It may already do this; eg "x-forwarded-for")


The other problem (in general) is that the IP address of your machine could well be a private IP address. It is not guaranteed that the IP will be addressable by the server. Or if the server is using it to identify the client, then there is no guarantee the IP will be unique ... or reliable; eg not spoofed in some way.


There is one alternative though. If you configure your server and client correctly, the server can rely on an SSL client certificate to establish the identity of the client. But this requires a few things:

  • The user's firewall must allow HTTPS connections.
  • The client application (or user's browser) must have a client certificate in its keystore.
  • The server must be able to trust the client certificate and know what identity it establishes (a person? a machine?)
  • The client application must know to send the certificate when establishing the HTTPS connection.

There are also security issues with sending SSL/TLS through a proxy. There is the potential for "man in the middle" attacks if the proxy can't be trusted.

All in all, this is a "difficult" approach.

You could query What is my ip address? , or any of the other websites that can tell you (eg google what is my ip address).

do like this

String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) {
    ipAddress = request.getRemoteAddr();
}
System.out.println("ipAddress:" + ipAddress);

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