Multi-level reverse proxy, Java multi-medium method integration for obtaining the real IP address of the Request Client

Source: Internet
Author: User

In JSP, the method for obtaining the Client IP address is request. getRemoteAddr (), which is valid in most cases. However, the real IP address of the client cannot be obtained through reverse proxy software such as Apache and Squid.

If reverse proxy software is used, when the URL reverse proxy of http: // 192.168.1.110: 2046/is set to the URL of the http://www.javapeixun.com.cn/, request is used. the IP address obtained by the getRemoteAddr () method is 127.0.0.1 or 192.168.1.110, but not the real IP address of the client.

After proxy, because the intermediate layer is added between the client and the service, the server cannot directly obtain the IP address of the client, and the server application cannot directly return the IP address of the forwarded request to the client. However, X-FORWARDED-FOR information is added in the HTTP header information that forwards the request. It is used to track the original Client IP address and the server address of the original client request. When we access http://www.javapeixun.com.cn/index. jsp/is not actually the index on the server accessed by our browser. the jsp file is first accessed by the Proxy Server http: // 192.168.1.110: 2046/index. jsp, the proxy server returns the access results to our browser, because the proxy server accesses the index. jsp, so index. in jsp, request. the IP obtained by getRemoteAddr () is actually the proxy server address, not the client IP address.

So we can obtain the real IP address of the client. Method 1:

 
 
  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. }

But when I access http://www.5a520.cn/index. in jsp/, the returned IP address is always unknown, and it is not 127.0.0.1 or 192.168.1.110 as shown above. I access http: // 192.168.1.110: 2046/index. jsp, the real IP address of the client can be returned, and a method is written for verification. The reason is Squid. The forwarded_for configuration file of squid. conf is on by default. If forwarded_for is set to off, X-Forwarded-For: unknown

So we can obtain the real IP address of the client. Method 2:

 
 
  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 a multi-level reverse proxy is passed, there will be more than one X-Forwarded-For value, but a string of Ip values. Which is the real IP address of the client?

The answer is to take the first valid IP string not unknown in X-Forwarded-.

For example, X-Forwarded-For: 192.168.1.110, 192.168.1.120, 192.168.1.130, and 192.168.1.100.

If the above method does not work, use the following method:

/*** Get the current network ip * @ param request * @ return */public String getIpAddr (HttpServletRequest request) {String ipAddress = request. getHeader ("x-forwarded-for"); if (ipAddress = null | ipAddress. length () = 0 | "unknown ". equalsIgnoreCase (ipAddress) {ipAddress = request. getHeader ("Proxy-Client-IP");} if (ipAddress = null | ipAddress. length () = 0 | "unknown ". equalsIgnoreCase (ipAddress) {ipAddress = reque St. getHeader ("WL-Proxy-Client-IP");} if (ipAddress = null | ipAddress. length () = 0 | "unknown ". equalsIgnoreCase (ipAddress) {ipAddress = request. getRemoteAddr (); if (ipAddress. equals ("127.0.0.1") {// obtain the IPInetAddress inet = null configured on the local machine based on the NIC; try {inet = InetAddress. getLocalHost ();} catch (UnknownHostException e) {e. printStackTrace ();} ipAddress = inet. getHostAddress () ;}/// when multiple proxies are used, the first IP address is the real IP address of the client. Cut if (ipAddress! = Null & ipAddress. length ()> 15 ){//"***. ***. ***. ***". length () = 15if (ipAddress. indexOf (",")> 0) {ipAddress = ipAddress. substring (0, ipAddress. indexOf (",") ;}} return ipAddress ;}

The above content is from the network. You just integrate it. Hope to help you!

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.