Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to obtain client ip address by java

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people don't understand the knowledge points of this article "how java realizes obtaining client ip address", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "how java realizes obtaining client ip address".

I. Foreword

Environment: jdk1.8 + idea2019.3 + Windows10

II. Summary

In project development, when daily processing operation log, user operation log recording is basically realized through aop section, but when it comes to recording operation log, there must be one item that basically refers to the record, that is, the client ip address of the operator. It is also convenient to check "dirty" in the future.

So here's the problem. How do I get the IP address of the client? Hahaha, this is the teaching content of this period. If some friends know how to obtain it, then I will praise you, but is the realization idea different from mine? So you can also try to see how bugs work.

Now, I'm gonna start teaching, and you're gonna have to listen.

Java implementation to get client ip

Step 1: Let's define a utility class that encapsulates ip-related method classes.

package com.example.review.util; import org.apache.commons.lang3.StringUtils;import javax.servlet.http.HttpServletRequest; /** * ip related tools * * @Author luoYong * @Date 2022-03-30 17:16 */public class IpUtils { }

Step 2: that is the core of this teaching, then how to get the client ip? I'm not going to keep you in suspense.

Usually get the IP address of the client is through request.getRemoteAddr(), right, but you have thought about it, now the basic system will carry out domain name proxy, such as through Apache,Squid and other reverse proxy software, using getRemoteAddr() simply can not get the real IP address of the client.

Why can't I get it if I add an agent? This is because an intermediate proxy is added between the client and the service, so the server cannot directly get the IP address of the client, and the server-side application cannot directly return the address of the request to the client. Basically, this acquisition method is directly passed.

If you still don't understand, you can look at the following agent flow diagram and you will understand it.

If only the limited system does not act as an agent, it must be ok. It's basically impossible for the system not to act as an agent, so what should we do? Don't worry, I'll tell you how to play, if you can't get it using my teaching method, please beat me, okay?

The following is the specific access to client ip method class: for your reference only.

/** * * Get client ip address * @param request */ public static String getIP(final HttpServletRequest request) throws Exception { if (request == null) { throw (new Exception("getIpAddr method HttpServletRequest Object is null")); } String ipStr = request.getHeader("x-forwarded-for"); if (StringUtils.isBlank(ipStr) || "unknown".equalsIgnoreCase(ipStr)) { ipStr = request.getHeader("Proxy-Client-IP"); } if (StringUtils.isBlank(ipStr) || "unknown".equalsIgnoreCase(ipStr)) { ipStr = request.getHeader("WL-Proxy-Client-IP"); } if (StringUtils.isBlank(ipStr) || "unknown".equalsIgnoreCase(ipStr)) { ipStr = request.getRemoteAddr(); } //If there are multiple routes, take the first unknown ip final String[] arr = ipStr.split(","); for (final String str : arr) { if (! "unknown".equalsIgnoreCase(str)) { ipStr = str; break; } } //The purpose is to convert localhost access ip 0:0:0:0:0:1 to 127.0.0.1. return ipStr.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ipStr; }

It should be noted that their own local development, basically by using the [ localhost ] standard host name to access the interface, right, then you will be particularly curious, the database log ip this column, ip some unexpectedly is 0:0:0:0:1.

Why is the IP obtained like this? This is because 0:0:0:0:0:0:1 is the expression of [ ipv6 ], which corresponds to [ ipv4 ] equivalent to 127.0.0.1, which is native. So I finally forced the IPv6 address to be converted to IPv4 address through the three-eye operation. Can you understand this? If you don't understand, you can go back and make up for the computer network. This is not impossible.

The following are the operation log records stored in the database for interface access through localhost and actual ip respectively.

Attached is a screenshot of the local ip database:

The above is the content of this article about "java how to get client ip address". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will help everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report