| 
					
				 | 
			
			
				@ -0,0 +1,255 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				package com.yihu.iot.service.platform; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.alibaba.fastjson.JSONObject; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.iot.dao.company.IotCompanyAppDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.iot.dao.platform.IotInterfaceLogDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.iot.dao.workType.IotWorkTypeDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.iot.service.useragent.UserAgent; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.entity.iot.company.IotCompanyAppDO; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.entity.iot.platform.IotInterfaceLogDO; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.entity.iot.workType.IotWorkTypeDO; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.restmodel.iot.platform.IotInterfaceLogVO; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.restmodel.web.MixEnvelop; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.rm.iot.IotRequestMapping; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.mysql.query.BaseJpaService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.apache.commons.lang.StringUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.slf4j.Logger; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.slf4j.LoggerFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.beans.BeanUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.beans.factory.annotation.Autowired; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.jdbc.core.BeanPropertyRowMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.jdbc.core.JdbcTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.springframework.stereotype.Service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import javax.servlet.http.HttpServletRequest; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.net.InetAddress; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.net.UnknownHostException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.text.DecimalFormat; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.text.ParseException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				/** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 * @author HZY 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 * @vsrsion 1.0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 * Created at 2020/4/28 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				@Service 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				public class IotInterfaceLogService extends BaseJpaService<IotInterfaceLogDO, IotInterfaceLogDao> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private static final Logger logger = LoggerFactory.getLogger(IotInterfaceLogService.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private IotInterfaceLogDao iotInterfaceLogDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private UserAgent userAgent; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private JdbcTemplate jdbcTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private IotCompanyAppDao iotCompanyAppDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private IotWorkTypeDao iotWorkTypeDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 分页查询日志信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param page 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param size 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @throws ParseException 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public MixEnvelop<IotInterfaceLogVO,IotInterfaceLogVO> findAll(Integer page, Integer size) throws ParseException { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String sql = "select * from iot_interface_log where 1=1;"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String sorts = "-updateTime";//按更新时间降序 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//        List<IotInterfaceLogDO> list = search(null, filters, sorts, page, size); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<IotInterfaceLogDO> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper(IotInterfaceLogDO.class)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        ArrayList<IotInterfaceLogVO> resultList = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        list.forEach(one->{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            IotInterfaceLogVO target = new IotInterfaceLogVO(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            BeanUtils.copyProperties(one,target); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            resultList.add(target); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        long count =list.size(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return MixEnvelop.getSuccessListWithPage(IotRequestMapping.Platform.message_success_find,resultList,page, size,count); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 数据消费 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param page 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param size 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public MixEnvelop<IotInterfaceLogVO,IotInterfaceLogVO> dataConsumption(Integer page,Integer size){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				/*        //根据应用和接口确定唯一的接口被调用次数 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String sqlTotal="SELECT COUNT(id) num,app_name,interface_name,work_type FROM iot_interface_log GROUP BY app_name,interface_name;"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //根据应用和接口确定唯一的接口被调用的失败次数 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String sql="SELECT COUNT(id) num,app_name,interface_name,work_type FROM iot_interface_log  WHERE state=0 GROUP BY app_name,interface_name;"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<IotInterfaceLogVO> list = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<Map<String, Object>> total = jdbcTemplate.queryForList(sqlTotal); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<Map<String, Object>> failCount = jdbcTemplate.queryForList(sql); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        total.forEach(one->{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            IotInterfaceLogVO iotInterfaceLogVO = new IotInterfaceLogVO(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            iotInterfaceLogVO.setAppName(one.get("app_name").toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            iotInterfaceLogVO.setInterfaceName(one.get("interface_name").toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            iotInterfaceLogVO.setWorkType(one.get("work_type").toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            iotInterfaceLogVO.setCount(one.get("num").toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            list.add(iotInterfaceLogVO); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //获取总的调用次数 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Long totalCount = (long) list.size(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //获取失败次数 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (failCount==null){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            list.forEach(one->{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                one.setFailureRate("0.00"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            return MixEnvelop.getSuccessListWithPage(IotRequestMapping.Platform.message_success_find,list,page, size,totalCount); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        list.forEach(iotInterfaceLogVO->{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            failCount.forEach(one->{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                if(iotInterfaceLogVO.getAppName().equalsIgnoreCase(one.get("app_name").toString()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                        && iotInterfaceLogVO.getInterfaceName().equalsIgnoreCase(one.get("interface_name").toString()) ){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String num = one.get("num").toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    Double value = Double.valueOf(num); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String count = iotInterfaceLogVO.getCount(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    Double all = Double.valueOf(count); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    DecimalFormat df=new DecimalFormat("0.00");//设置保留位数 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String format = df.format(value / all); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    iotInterfaceLogVO.setFailureRate(format); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return MixEnvelop.getSuccessListWithPage(IotRequestMapping.Platform.message_success_find,list,page, size,totalCount);*/ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String sql ="SELECT  COALESCE(c.fali,0),b.count,b.app_name,b.interface_name,b.work_type FROM " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                "                                        (SELECT COUNT(id) fali,app_name,interface_name FROM iot_interface_log  WHERE state=0 GROUP BY app_name,interface_name) c " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                                         "RIGHT JOIN (SELECT count(id) count,app_name,interface_name,work_type FROM iot_interface_log " + 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                                         " GROUP BY app_name,interface_name) b ON c.app_name=b.app_name AND c.interface_name=b.interface_name;"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<Map<String, Object>> listSql = jdbcTemplate.queryForList(sql); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<IotInterfaceLogVO> list = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        listSql.forEach(one->{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            IotInterfaceLogVO iotInterfaceLogVO = new IotInterfaceLogVO(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            iotInterfaceLogVO.setAppName(one.get("app_name").toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            iotInterfaceLogVO.setInterfaceName(one.get("interface_name").toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            iotInterfaceLogVO.setWorkType(one.get("work_type").toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            iotInterfaceLogVO.setCount(one.get("count").toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            iotInterfaceLogVO.setFailureRate(Float.toString(Float.parseFloat(one.get("COALESCE(c.fali,0)").toString())/Float.parseFloat(one.get("count").toString()))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            list.add(iotInterfaceLogVO); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        long totalCount=listSql.size(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return MixEnvelop.getSuccessListWithPage(IotRequestMapping.Platform.message_success_find,list,page, size,totalCount); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 保存接口调用日志 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param appId 应用ID 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param params 入参 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param response 出参 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param request 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param state 调用状态 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param interfaceName 接口名称 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param method 方法名 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param workTypeId 业务类型 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param time 调用时间 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public void saveLog(String appId, String params, String response, HttpServletRequest request,Integer state,String  interfaceName,String method,String workTypeId,Date time){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        IotInterfaceLogDO interfaceLogDO = new IotInterfaceLogDO(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //设置应用与公司 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        IotCompanyAppDO appDO = iotCompanyAppDao.findById(appId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setAppId(appId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setAppName(appDO.getName()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setCompanyId(appDO.getCompanyId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setCompanyName(appDO.getCompanyName()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //设置业务类型 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        IotWorkTypeDO work = iotWorkTypeDao.findById(workTypeId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setWorkType(work.getName()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setExplanation(work.getExplanation()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setWorkTypeId(workTypeId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //设置用户名称与ID 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if(StringUtils.isBlank(userAgent.getUID())){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            interfaceLogDO.setUserId("system"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            interfaceLogDO.setUserName("system"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }else{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            interfaceLogDO.setUserId(userAgent.getUID()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            interfaceLogDO.setUserId(userAgent.getUNAME()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setAddressIp(getIpAddress(request)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setUrl(request.getRequestURL().toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        HashMap<String, String> headMap = new HashMap<>(16); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Enumeration<String> headerNames = request.getHeaderNames(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        while (headerNames.hasMoreElements()){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String name = headerNames.nextElement(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            String value = request.getHeader(name); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            headMap.put(name,value); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String headJson = JSONObject.toJSONString(headMap); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setHead(headJson); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setRequest(params); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setResponse(response); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setState(state); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setInterfaceName(interfaceName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setMethod(method); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        interfaceLogDO.setTime(time); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        iotInterfaceLogDao.save(interfaceLogDO); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址。 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param request 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private String getIpAddress(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.getHeader("HTTP_CLIENT_IP"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            ip = request.getHeader("HTTP_X_FORWARDED_FOR"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            ip = request.getRemoteAddr(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if("127.0.0.1".equals(ip)||"0:0:0:0:0:0:0:1".equals(ip)){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                //根据网卡取本机配置的IP 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                InetAddress inet=null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    inet = InetAddress.getLocalHost(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } catch (UnknownHostException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                ip= inet.getHostAddress(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return ip; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				} 
			 |