一、前言
为了防止本站资源(小木人印象www.xwood.net)被恶意下载,最近实现安全控制模块-通过分析用户访问IP地址在有效时间内的对本站资源合理下载量,作为黑名单规则,但是发现获取通过之前HttpClientIpUtils工具类获取的ip地址都是127.0.0.1,无法获取终端访问用户有效的ip地址,导致黑名单库无法创建。
二、解决方法
由于nginx配置服务端的反向代理导致,之前反向配置如下
location ^~/open-api/{@b@ proxy_pass http://127.0.0.1:8080/openapi/; @b@}
应该调整配置为如下(增加配置项proxy_set_header x-forwarded-for $remote_addr;)
location ^~/open-api/{@b@ proxy_pass http://127.0.0.1:8080/openapi/;@b@ proxy_set_header x-forwarded-for $remote_addr;@b@}
三、黑名单代码分享
1.访问客户端安全控制类ClientUserController,代码如下
public class ClientUserController {@b@ @b@ private static final Logger logger = Logger.getLogger(ClientUserController.class);@b@ private static ConcurrentMap<String,ClientUser> downloadUsers=new ConcurrentHashMap<String,ClientUser>();@b@ private static List<String> blackIplist=new CopyOnWriteArrayList<String>();@b@ @b@ //12小时最大下载量@b@ private static int maxDayDownloadTimes=1000;@b@ @b@ //验证期限@b@ private static long validTimeSec=12*60*60;@b@ @b@ public static void register(String ip){@b@ @b@ if(StringUtils.isEmpty(ip)||"127.0.0.1".equalsIgnoreCase(ip))@b@ return ;@b@ @b@ if(!isPermission(ip))@b@ return ;@b@ @b@ if(downloadUsers.containsKey(ip)){@b@ downloadUsers.get(ip).setDownloadTimes(downloadUsers.get(ip).getDownloadTimes()+1);@b@ logger.info(" downloadUser login --------------:"+ip+" times----------------:"+downloadUsers.get(ip).toString());@b@ }else{@b@ downloadUsers.put(ip,new ClientUser(ip));@b@ logger.info(" New downloadUser register --------------:"+ip+" times----------------:1");@b@ }@b@ @b@ }@b@ @b@ @b@ public static boolean isPermission(String ip){@b@ @b@ if(StringUtils.isEmpty(ip)){@b@ logger.info(" downloadUser isPermission false,becase you have't clientIp <<<<<<<<<<<<<<<<<<<<<<<< ");@b@ return false;@b@ }@b@ @b@ if("127.0.0.1".equalsIgnoreCase(ip)){@b@ logger.info(" downloadUser can't get ip ; ======================================== 127.0.0.1 ");@b@ return true;@b@ }@b@ @b@ @b@ if(blackIplist.contains(ip)){@b@ logger.info(" downloadUser@"+ip+"@ is danger downloadUser !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");@b@ logger.info(" downloadUser@"+ip+"@ is danger downloadUser !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");@b@ logger.info(" downloadUser@"+ip+"@ is danger downloadUser !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");@b@ return false;@b@ }@b@ @b@ if(downloadUsers.containsKey(ip)){@b@ @b@ ClientUser checkClientUser=downloadUsers.get(ip);@b@ @b@ if(System.currentTimeMillis()-checkClientUser.getLastTime()>=validTimeSec){@b@ @b@ if(checkClientUser.getDownloadTimes()>=maxDayDownloadTimes){@b@ blackIplist.add(ip);@b@ logger.info(" downloadUser@"+ip+"@ add to blacklist !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");@b@ logger.info(" downloadUser@"+ip+"@ add to blacklist !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");@b@ logger.info(" downloadUser@"+ip+"@ add to blacklist !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");@b@ return false;@b@ }else{@b@ downloadUsers.remove(ip);@b@ }@b@ @b@ }else{@b@ @b@ if(checkClientUser.getDownloadTimes()>=maxDayDownloadTimes){@b@ blackIplist.add(ip);@b@ logger.info(" downloadUser@"+ip+"@ add to blacklist !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");@b@ logger.info(" downloadUser@"+ip+"@ add to blacklist !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");@b@ logger.info(" downloadUser@"+ip+"@ add to blacklist !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");@b@ logger.info(" downloadUser@"+ip+"@ add to blacklist !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");@b@ logger.info(" downloadUser@"+ip+"@ add to blacklist !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");@b@ logger.info(" downloadUser@"+ip+"@ add to blacklist !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");@b@ return false;@b@ }@b@ @b@ }@b@ @b@ @b@ }@b@ @b@ return true;@b@ }@b@ @b@@b@}
2.客户端用户类ClientUser,代码如下
public class ClientUser {@b@ @b@ private String ip;@b@ @b@ private Integer downloadTimes=1;@b@ @b@ private Long lastTime;@b@ @b@ public ClientUser() {@b@ super();@b@ lastTime=System.currentTimeMillis();@b@ }@b@@b@ public ClientUser(String ip) {@b@ super();@b@ this.ip = ip;@b@ lastTime=System.currentTimeMillis();@b@ }@b@@b@ public String getIp() {@b@ return ip;@b@ }@b@@b@ public void setIp(String ip) {@b@ this.ip = ip;@b@ }@b@@b@ public Integer getDownloadTimes() {@b@ return downloadTimes;@b@ }@b@@b@ public void setDownloadTimes(Integer downloadTimes) {@b@ this.downloadTimes = downloadTimes;@b@ }@b@@b@ public Long getLastTime() {@b@ return lastTime;@b@ }@b@@b@ public void setLastTime(Long lastTime) {@b@ this.lastTime = lastTime;@b@ }@b@ @b@ @b@ public static void main(String[] args) throws Exception{@b@ ClientUser u=new ClientUser();@b@ u.lastTime=System.currentTimeMillis();@b@ Thread.sleep(2000);@b@ System.out.println((System.currentTimeMillis()-u.lastTime)/1000);@b@ }@b@ @b@ @Override@b@ public String toString() {@b@ return "ClientUser [ip=" + ip + "]";@b@ }@b@@b@ @Override@b@ public boolean equals(Object obj) {@b@ @b@ ClientUser _this=(ClientUser)obj;@b@ if(_this==null)@b@ return false;@b@ @b@ if(this.getIp().equalsIgnoreCase(_this.getIp()))@b@ return true;@b@@b@ return false;@b@ }@b@@b@}