Home  >  Article  >  Java  >  How to get the client's IP address in Java?

How to get the client's IP address in Java?

WBOY
WBOYforward
2023-04-26 17:25:074728browse

1. Foreword

Environment: jdk1.8 idea2019.3 Windows10

2. Summary

In project development, when dealing with operation logs on a daily basis, the recording of user operation logs is basically achieved through the AOP aspect. However, when it comes to recording operation logs, there must be one item that is basically referenced and recorded, and that is the operator's customers. It is also convenient to check the "dirty" IP address in the future.

Then the question comes. How do you specifically get the client’s IP address? Hahaha, this is my teaching content in this issue. If some friends know how to get it, then I want to praise you, but are the implementation ideas different from mine? So you can also try to see how bug bacteria are implemented.

Next, I will start teaching, you have to listen carefully.

3. Java implementation to obtain client ip

Step 1: Let’s first define a tool class specifically used to encapsulate ip-related method classes .

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

The second step: This is the core of this teaching, so how to obtain the client IP? I won’t sell it any more.

Usually the client’s IP address is obtained through request.getRemoteAddr() , right, but have you ever thought about it? Nowadays, basic systems will perform domain name proxy, such as through Apache, For reverse proxy software such as Squid, it is no longer possible to obtain the client's real IP address using getRemoteAddr().

Why can’t I get it after adding an agent? This is because an intermediary proxy is added between the client and the service, so the server cannot directly obtain the client's IP address, and the server-side application cannot directly return it to the client by forwarding the requested address. Basically, this acquisition method is directly Was passed.

If you still don’t understand, you can look at the agency process diagram below and you will understand.

How to get the clients IP address in Java?

This If you only limit the system and do not act as an agent, it must be OK. It is basically impossible for the system not to act as an agent, so what should we do? Don't worry, I will tell you how to play. If you still can't get it after using my teaching methods, please come and beat me up, okay?

The following is the specific method class for obtaining the client IP: for your reference only.

    /***
     * 获取客户端ip地址
     * @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();
        }
 
        // 多个路由时,取第一个非unknown的ip
        final String[] arr = ipStr.split(",");
        for (final String str : arr) {
            if (!"unknown".equalsIgnoreCase(str)) {
                ipStr = str;
                break;
            }
        }
        //目的是将localhost访问对应的ip 0:0:0:0:0:0:0:1 转成 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 when developing locally, you basically use the [localhost] standard host name for interface access, right? Then you will be particularly curious about the database record log IP column. , some IPs are actually 0:0:0:0:0:0:0:1.

How to get the clients IP address in Java?

Why is the IP obtained like this? This is because 0:0:0:0:0:0:0:1 is the representation of [ipv6], which corresponds to [ipv4] and is equivalent to 127.0.0.1, which is the local machine. So in the end, I forcibly converted the ipv6 address to an ipv4 address through trinocular arithmetic. Can everyone understand this? If you don’t understand, it’s not impossible to go back and tinker with the computer network.

The following are the operation log records saved in the database for interface access through localhost and actual IP.

Attached is a screenshot of the local IP database:

How to get the clients IP address in Java?

The above is the detailed content of How to get the client's IP address in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete