博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java检测http请求的ip地址 Java问题通用解决代码
阅读量:5947 次
发布时间:2019-06-19

本文共 2848 字,大约阅读时间需要 9 分钟。

以后再用到的话,至少能起个参考作用
java实现,struts2的Action中,依赖HttpServletRequest
 
 

package 
net.dookoo.web.action;
 
import 
java.util.Enumeration;
 
import 
javax.servlet.http.HttpServletRequest;
 
import 
com.linktong.webframework.action.BaseAction;
 
/**
 * 检测系列操作
 *
 * 
@author 
sven
 * 
@version 
1.0(新版本号)
 * 
@see 
参考的JavaDoc
 */
public 
class 
CheckAction 
extends 
BaseAction {
 
      
@Override
      
public 
int 
getObjCount() {
            
// 
TODO 
Auto-generated method stub
            
return 
0;
      }
 
      
public 
String getIpAddrByRequest(HttpServletRequest request) {
            
// String ip = request.getHeader("x-forwarded-for");
 
            String ip = 
null
;
            String localIP = 
"127.0.0.1"
;
                  
            
// 反向代理标识不区分大小写,防止配置问题
            Enumeration enumeration = 
null
;
            
//          enumeration = request.getHeaderNames();
//          System.out.println("header----------------");
//          while (enumeration.hasMoreElements()) {
//                String headerName = (String) enumeration.nextElement();
//                System.out.println(headerName+"-"+request.getHeader(headerName));
//          }
//          System.out.println("header----------------");
            
            enumeration = request.getHeaderNames();
            
while 
(enumeration.hasMoreElements()) {
                  String headerName = (String) enumeration.nextElement();
                  
                  
if 
(
"cdn-src-ip"
.equalsIgnoreCase(headerName)) {
                        ip = request.getHeader(headerName);
//                      System.out.println("headerName:"+headerName);
                        
break
;
                  }
            }
//          System.out.println("cdn-src-ip:" + ip);
            
            enumeration = request.getHeaderNames();
            
if 
(ip == 
null 
|| ip.length() == 0 || (ip.equalsIgnoreCase(localIP)) || 
"unknown"
.equalsIgnoreCase(ip)) {
                  
while 
(enumeration.hasMoreElements()) {
                        String headerName = (String) enumeration.nextElement();
                        
                        
if 
(
"x-forwarded-for"
.equalsIgnoreCase(headerName)) {
                              ip = request.getHeader(headerName);
//                            System.out.println("headerName:"+headerName);
                              
break
;
                        }
                  }
            }
//          System.out.println("x-forwarded-for:" + ip);
            
            
if 
(ip == 
null 
|| ip.length() == 0 || (ip.equalsIgnoreCase(localIP)) || 
"unknown"
.equalsIgnoreCase(ip)) {
                  ip = request.getHeader(
"Proxy-Client-IP"
);
            }
//          System.out.println("Proxy-Client-IP:" + request.getHeader("Proxy-Client-IP"));
 
            
            
if 
(ip == 
null 
|| ip.length() == 0 || (ip.equalsIgnoreCase(localIP)) || 
"unknown"
.equalsIgnoreCase(ip)) {
                  ip = request.getHeader(
"WL-Proxy-Client-IP"
);
            }
//          System.out.println("WL-Proxy-Client-IP:" + request.getHeader("WL-Proxy-Client-IP"));
 
            
            
if 
(ip == 
null 
|| ip.length() == 0 || (ip.equalsIgnoreCase(localIP)) || 
"unknown"
.equalsIgnoreCase(ip)) {
                  ip = request.getRemoteAddr();
            }
//          System.out.println("RemoteAddr:" + request.getRemoteAddr());
 
            
return 
ip;
      }
 
      
public 
String checkAnonymityIP() {
            
try 
{
                  String ipAddr = getIpAddrByRequest(getRequest());
                  
this
.debugTrace(
"check"
, 
"检测到请求的ip地址为" 
+ ipAddr);
                  
// System.out.println(ipAddr);
 
                  getRequest().setAttribute(
"ipAddr"
, ipAddr);
            } 
catch 
(Throwable e) {
                  
this
.errorTrace(
"check"
, e.getMessage(), e);
                  getRequest().setAttribute(
this
.
ERRORMESSAGE
, 
this
.
SYSTEMERROR
);
                  
return 
this
.
ERROR
;
            }
            
return 
this
.
SUCCESS
;
      }
 
}

转载地址:http://nefxx.baihongyu.com/

你可能感兴趣的文章
好程序员分享大势所趋 HTML5成Web开发者最关心的技术 ...
查看>>
北汽集团荣辉:抓不住自动驾驶 就抓不住车企的命脉 | 自动驾驶这十年 ...
查看>>
豆瓣评分8.8,这本程序员案头必备宝典,10年沉淀,新版再现 ...
查看>>
运行 Spring Boot 应用的 3 种方式!
查看>>
【内容安全】虚拟化及云环境下数据库审计优缺点分析
查看>>
crmeb电商系统
查看>>
xttprep.tmpl
查看>>
mycat垂直分库
查看>>
无需停机,手把手教您将 Docker CE 切换为 Docker EE
查看>>
Ubuntu 14.04 Web服务器,Apache的安装和配置
查看>>
MaxCompute 图计算用户手册(上)
查看>>
自带科技基因,打造纯原创IP,“燃烧小宇宙”获数千万A轮融资
查看>>
未能加载文件或程序集"Newtonsoft.Json, Version=4.5.0.0
查看>>
C#多线程编程系列(二)- 线程基础
查看>>
Jenkins 内置变量(学习笔记二十四)
查看>>
PostgreSQL 10.1 手册_部分 II. SQL 语言_第 13 章 并发控制_13.2. 事务隔离
查看>>
虚拟机概念
查看>>
【云周刊】第195期:全球首家!阿里云获GNTC2018 网络创新大奖 成唯一获奖云服务商...
查看>>
【VS】使用vs2017自带的诊断工具(Diagnostic Tools)诊断程序的内存问题
查看>>
AutoScaling 支持从实例启动模板创建实例
查看>>