Java can be used to obtain the real IP address of a 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. <Br/> tag: real IP address of the Java client <br/> in JSP, the method for obtaining the IP address of the client is request. getremoteaddr (), which is effective in most cases. However, the real IP address of the client cannot be obtained through reverse proxy software such as Apache and squid. <Br/> if reverse proxy software is used, request 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. 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. </P> <p> after proxy, the server cannot directly obtain the IP address of the client because an intermediate layer is added between the client and the service, the server application cannot directly return the request address 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. </P> <p> method 1 for obtaining the real IP address of the client: </P> <p> Public String getremortip (httpservletrequest request) {<br/> If (request. getheader ("X-forwarded-for") = NULL) {<br/> return request. getremoteaddr (); <br/>}< br/> return request. getheader ("X-forwarded-for"); <br/>}</P> <p> 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, you can Return the real IP address of the client and write a method for verification. The reason is squid. Squid. the configuration file forwarded_for in conf is on by default. If forwarded_for is set to off, X-forwarded-: unknown </P> <p> Method 2: </P> <p> Public String getipaddr (httpservletrequest request) {<br/> string IP = request. getheader ("X-forwarded-for"); <br/> If (IP = NULL | IP. length () = 0 | "unknown ". equalsignorecase (IP) {<br/> IP = request. getheader ("proxy-client-IP"); <br/>}< br/> If (IP = NULL | IP. lengt H () = 0 | "unknown ". equalsignorecase (IP) {<br/> IP = request. getheader ("wl-proxy-client-IP"); <br/>}< br/> If (IP = NULL | IP. length () = 0 | "unknown ". equalsignorecase (IP) {<br/> IP = request. getremoteaddr (); <br/>}< br/> return IP; <br/>}</P> <p> however, if a multi-level reverse proxy is passed, x-forwarded-For has more than one value, but a string of IP values. Which one is the real IP address of the client? </P> <p> the answer is to take the first non-unknown valid IP string in X-forwarded-. </P> <p> for example, X-forwarded-for: 192.168.1.110, 192.168.1.120, 192.168.1.130, and 192.168.1.100 real IP addresses: 192.168.1.110 <br/>

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.