如题所述
é®é¢å¼åºï¼
<hr/>
å¨JSPéï¼è·å客æ·ç«¯çIPå°åçæ¹æ³æ¯ï¼request.getRemoteAddr()ï¼è¿ç§æ¹æ³å¨å¤§é¨åæ åµä¸é½æ¯ææçãä½æ¯å¨éè¿äº Apacheï¼Nagixçåå代ç软件就ä¸è½è·åå°å®¢æ·ç«¯ççå®IPå°åäºãå¦æ使ç¨äºåå代ç软件ï¼ç¨ request.getRemoteAddr()æ¹æ³è·åçIPå°åæ¯ï¼127.0.0.1æ 192.168.1.110ï¼è并ä¸æ¯å®¢æ·ç«¯ççå®IPã
ç»è¿ä»£ç以åï¼ç±äºå¨å®¢æ·ç«¯åæå¡ä¹é´å¢å äºä¸é´å±ï¼å æ¤æå¡å¨æ æ³ç´æ¥æ¿å°å®¢æ·ç«¯ç IPï¼æå¡å¨ç«¯åºç¨ä¹æ æ³ç´æ¥éè¿è½¬å请æ±çå°åè¿åç»å®¢æ·ç«¯ãä½æ¯å¨è½¬å请æ±çHTTP头信æ¯ä¸ï¼å¢å äºX-FORWARDED-FORä¿¡æ¯ãç¨ä»¥è·è¸ªåæç客æ·ç«¯ IPå°åååæ¥å®¢æ·ç«¯è¯·æ±çæå¡å¨å°åã
举ä¾æ¥è¯´ï¼å½æ们访é®å£ç¢ç½é¦é¡µhangzhou.jspæ¶ï¼å ¶å®å¹¶ä¸æ¯æ们æµè§å¨çæ£è®¿é®å°äºæå¡å¨ä¸çhangzhou.jsp æ件ï¼èæ¯å ç±ä»£çæå¡å¨Nagixå»è®¿é®hagnzhou.jsp ï¼ä»£çæå¡å¨åå°è®¿é®å°çç»æè¿åç»æ们çæµè§å¨ï¼å 为æ¯ä»£çæå¡å¨å»è®¿é®hangzhou.jspçï¼æ以hangzhou.jspä¸éè¿ request.getRemoteAddr()çæ¹æ³è·åçIPå®é ä¸æ¯ä»£çæå¡å¨çå°åï¼å¹¶ä¸æ¯å®¢æ·ç«¯çIPå°åã
<hr/>
è·å¾å®¢æ·ç«¯çå®IPå°åçæ¹æ³ä¸ï¼
public String getRemortIP(HttpServletRequest request) {
ãã if (request.getHeader("x-forwarded-for") == null) {
ãã return request.getRemoteAddr();
ãã }
ãã return request.getHeader("x-forwarded-for");
ãã}
<hr/>
è·å¾å®¢æ·ç«¯çå®IPå°åçæ¹æ³äº
public String getIpAddr(HttpServletRequest request) {
ãã String ip = request.getHeader("x-forwarded-for");
ãã if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ãã ip = request.getHeader("Proxy-Client-IP");
ãã }
ãã if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ãã ip = request.getHeader("WL-Proxy-Client-IP");
ãã }
ãã if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ãã ip = request.getRemoteAddr();
ãã }
ãã return ip;
ãã}
<hr/>
<hr/>
å¨JSPéï¼è·å客æ·ç«¯çIPå°åçæ¹æ³æ¯ï¼request.getRemoteAddr()ï¼è¿ç§æ¹æ³å¨å¤§é¨åæ åµä¸é½æ¯ææçãä½æ¯å¨éè¿äº Apacheï¼Nagixçåå代ç软件就ä¸è½è·åå°å®¢æ·ç«¯ççå®IPå°åäºãå¦æ使ç¨äºåå代ç软件ï¼ç¨ request.getRemoteAddr()æ¹æ³è·åçIPå°åæ¯ï¼127.0.0.1æ 192.168.1.110ï¼è并ä¸æ¯å®¢æ·ç«¯ççå®IPã
ç»è¿ä»£ç以åï¼ç±äºå¨å®¢æ·ç«¯åæå¡ä¹é´å¢å äºä¸é´å±ï¼å æ¤æå¡å¨æ æ³ç´æ¥æ¿å°å®¢æ·ç«¯ç IPï¼æå¡å¨ç«¯åºç¨ä¹æ æ³ç´æ¥éè¿è½¬å请æ±çå°åè¿åç»å®¢æ·ç«¯ãä½æ¯å¨è½¬å请æ±çHTTP头信æ¯ä¸ï¼å¢å äºX-FORWARDED-FORä¿¡æ¯ãç¨ä»¥è·è¸ªåæç客æ·ç«¯ IPå°åååæ¥å®¢æ·ç«¯è¯·æ±çæå¡å¨å°åã
举ä¾æ¥è¯´ï¼å½æ们访é®å£ç¢ç½é¦é¡µhangzhou.jspæ¶ï¼å ¶å®å¹¶ä¸æ¯æ们æµè§å¨çæ£è®¿é®å°äºæå¡å¨ä¸çhangzhou.jsp æ件ï¼èæ¯å ç±ä»£çæå¡å¨Nagixå»è®¿é®hagnzhou.jsp ï¼ä»£çæå¡å¨åå°è®¿é®å°çç»æè¿åç»æ们çæµè§å¨ï¼å 为æ¯ä»£çæå¡å¨å»è®¿é®hangzhou.jspçï¼æ以hangzhou.jspä¸éè¿ request.getRemoteAddr()çæ¹æ³è·åçIPå®é ä¸æ¯ä»£çæå¡å¨çå°åï¼å¹¶ä¸æ¯å®¢æ·ç«¯çIPå°åã
<hr/>
è·å¾å®¢æ·ç«¯çå®IPå°åçæ¹æ³ä¸ï¼
public String getRemortIP(HttpServletRequest request) {
ãã if (request.getHeader("x-forwarded-for") == null) {
ãã return request.getRemoteAddr();
ãã }
ãã return request.getHeader("x-forwarded-for");
ãã}
<hr/>
è·å¾å®¢æ·ç«¯çå®IPå°åçæ¹æ³äº
public String getIpAddr(HttpServletRequest request) {
ãã String ip = request.getHeader("x-forwarded-for");
ãã if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ãã ip = request.getHeader("Proxy-Client-IP");
ãã }
ãã if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ãã ip = request.getHeader("WL-Proxy-Client-IP");
ãã }
ãã if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ãã ip = request.getRemoteAddr();
ãã }
ãã return ip;
ãã}
<hr/>
温馨提示:答案为网友推荐,仅供参考
第1个回答 2014-12-15
获得客户端真实IP地址的方法一:
public String getRemortIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
<hr/>
获得客户端真实IP地址的方法二
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
<hr/>
本答案来自于互联网,仅供参考学习作用
如果您对我的回答有不满意的地方,还请您继续追问;
答题不易,互相理解,互相帮助!
public String getRemortIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
<hr/>
获得客户端真实IP地址的方法二
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
<hr/>
本答案来自于互联网,仅供参考学习作用
如果您对我的回答有不满意的地方,还请您继续追问;
答题不易,互相理解,互相帮助!