Multi-level reverse proxy, Java gets the real IP address of the requesting client multiple method integration

Source: Internet
Author: User

In the JSP, the method to obtain the IP address of the client is: Request.getremoteaddr (), which is valid in most cases. However, the real IP address of the client cannot be obtained through the reverse proxy software such as Apache,squid.

If the reverse proxy software is used, the URL of the http://192.168.1.110:2046/is reverse proxy to http://www.javapeixun.com.cn/URL, The IP address obtained with the REQUEST.GETREMOTEADDR () method is: 127.0.0.1 or 192.168.1.110, not the real IP of the client.

After the agent, due to the addition of the middle tier between the client and the service, so the server can not directly get the client's IP, the server-side application can not directly forward the requested address to the client. However, the x-forwarded-for information is added to the HTTP header information of the forwarding request. Used to track the original client IP address and the server address of the original client request. When we visit http://www.javapeixun.com.cn/index.jsp/, it is not that our browser actually accesses the index.jsp file on the server, but first the proxy server accesses HTTP/ 192.168.1.110:2046/INDEX.JSP, the proxy server returns the result of the access to our browser, Because it is the proxy server to access the index.jsp, the IP index.jsp in the Request.getremoteaddr () method is actually the address of the proxy server, not the IP address of the client.

So we can get the real IP address of the client method one:

    1. Public String Getremortip (HttpServletRequest request) {
    2. if (Request.getheader ("x-forwarded-for") = = null) {
    3. return request.getremoteaddr ();
    4. }
    5. Return Request.getheader ("X-forwarded-for");
    6. }

When I visit http://www.5a520.cn/index.jsp/, however, the IP address returned is always unknown, not the 127.0.0.1 or 192.168.1.110 as shown above, and I visit http:// 192.168.1.110:2046/index.jsp, you can return the real IP address of the client and write a method to verify it. The reason was on the squid. The squid.conf configuration file Forwarded_for entry is on by default, if Forwarded_for is set to OFF: X-forwarded-for:unknown

Then we can obtain the client real IP address method two:

  1. Public String getipaddr (HttpServletRequest request) {
  2. String IP = request.getheader ("X-forwarded-for");
  3. if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {
  4. ip = Request.getheader ("Proxy-client-ip");
  5. }
  6. if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {
  7. ip = Request.getheader ("Wl-proxy-client-ip");
  8. }
  9. if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {
  10. ip = request.getremoteaddr ();
  11. }
  12. return IP;
  13. }

However, if through the multi-level reverse proxy, x-forwarded-for value and more than one, but a string of IP values, exactly which is the real client IP?

The answer is to take the first non-unknown valid IP string in x-forwarded-for.

such as: x-forwarded-for:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100 user real IP: 192.168.1.110

If the above method is not enough, use the following method:

[Java]View Plain Copy
  1. /**
  2. * Get current network IP
  3. * @param request
  4. * @return
  5. */
  6. Public String getipaddr (HttpServletRequest request) {
  7. String ipAddress = Request.getheader ("X-forwarded-for");
  8. if (ipAddress = = NULL | | ipaddress.length () = = 0 | | "Unknown". Equalsignorecase (ipAddress)) {
  9. ipAddress = Request.getheader ("Proxy-client-ip");
  10. }
  11. if (ipAddress = = NULL | | ipaddress.length () = = 0 | | "Unknown". Equalsignorecase (ipAddress)) {
  12. ipAddress = Request.getheader ("Wl-proxy-client-ip");
  13. }
  14. if (ipAddress = = NULL | | ipaddress.length () = = 0 | | "Unknown". Equalsignorecase (ipAddress)) {
  15. IpAddress = Request.getremoteaddr ();
  16. if (Ipaddress.equals ("127.0.0.1") | | | ipaddress.equals ("0:0:0:0:0:0:0:1")) {
  17. IP based on NIC for native configuration
  18. InetAddress Inet=null;
  19. try {
  20. inet = Inetaddress.getlocalhost ();
  21. } catch (Unknownhostexception e) {
  22. E.printstacktrace ();
  23. }
  24. Ipaddress= inet.gethostaddress ();
  25. }
  26. }
  27. For cases through multiple proxies, the first IP is the client real IP, multiple IPs according to ', ' split
  28. if (Ipaddress!=null && ipaddress.length () >15) {//"***.***.***.***". Length () = 15
  29. if (Ipaddress.indexof (",") >0) {
  30. ipAddress = ipaddress.substring (0,ipaddress.indexof (","));
  31. }
  32. }
  33. return ipAddress;
  34. }

Multi-level reverse proxy, Java gets the real IP address of the requesting client multiple method integration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.