办学质量监测教学评价系统
ageerle
2025-04-11 ac4c037634ab6234758a61adb328e8ec5b0848af
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package org.ruoyi.common.wechat.web.utils;
 
 
import org.apache.commons.lang3.StringUtils;
 
import javax.servlet.http.HttpServletRequest;
 
 
/**
 * @author WesleyOne
 * @create 2018/11/5
 */
public class IpUtil {
 
    /**
     * 获取用户真实IP(在安防或多层代理场景下)
     * @param request
     * @return
     */
    public static String getRealIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            //多次反向代理后会有多个ip值,第一个ip才是真实ip
            int index = ip.indexOf(",");
            if(index != -1){
                return ip.substring(0,index);
            }
        }
        ip = request.getHeader("X-Real-IP");
        if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            return ip;
        }
        return request.getRemoteAddr();
    }
}