IP地址取客户端的,优化

This commit is contained in:
季圣华
2017-09-06 23:54:11 +08:00
parent 60b87954a8
commit 179e874ee0
62 changed files with 94 additions and 62 deletions

View File

@@ -1,5 +1,6 @@
package com.jsh.util;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
@@ -266,8 +267,9 @@ public class Tools
{
return str.replace("'", "");
}
/**
* 获取用户IP地址
* 获取用户IP地址(停用)
* @return 用户IP
* @see [类、类#方法、类#成员]
*/
@@ -283,6 +285,36 @@ public class Tools
return "127.0.0.1";
}
}
/**
* 从Request对象中获得客户端IP处理了HTTP代理服务器和Nginx的反向代理截取了ip
* @param request
* @return ip
*/
public static String getLocalIp(HttpServletRequest request) {
String remoteAddr = request.getRemoteAddr();
String forwarded = request.getHeader("X-Forwarded-For");
String realIp = request.getHeader("X-Real-IP");
String ip = null;
if (realIp == null) {
if (forwarded == null) {
ip = remoteAddr;
} else {
ip = remoteAddr + "/" + forwarded.split(",")[0];
}
} else {
if (realIp.equals(forwarded)) {
ip = realIp;
} else {
if(forwarded != null){
forwarded = forwarded.split(",")[0];
}
ip = realIp + "/" + forwarded;
}
}
return ip;
}
/**
* 转化前台批量传入的ID值

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
String type = request.getParameter("type");
String location = "首页";
if(null != type)

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -2,7 +2,7 @@
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html>
<html>

View File

@@ -3,7 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String clientIp = Tools.getCurrentUserIP();
String clientIp = Tools.getLocalIp(request);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">