昨天写了个Filter!
昨天看了看自己的资源存放站点,发现当机了,很奇怪:都是静态的东西怎么会当机?
看了看日志,NND,日志10几个G,再仔细看,都是一个IP发请求,获得一个文件,每秒4次,可怜我的TOMCAT,怎么撑得住这么搞?
再仔细想,不对啊!下一个文件只是一个请求即可,着明显是恶意攻击!
没办法,写了个Filter,只允许校内访问。
列举如下,备忘。
---------------------
package me;
/*
* Author: Elpout
* DateTime: 2008-06-18 17:46
* Lisence: Just You Like!
*
*/
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class IPFilter implements Filter {
protected FilterConfig filterConfig = null;
protected String[] AIP = null;
protected String AllowedIP = null;
protected String ErrorPage = null;
public void destroy() {
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
Boolean Flag = false;
String cip=req.getRemoteHost();
for (String TB:AIP){
if (cip.startsWith(TB)) {
Flag = true;
break;
}
}
if (Flag)
chain.doFilter(request, response);
else
resp.sendRedirect (this.ErrorPage);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.AllowedIP = filterConfig.getInitParameter("AllowedIP");
this.ErrorPage = filterConfig.getInitParameter("RePage");
this.AIP = AllowedIP.split(";");
}
}
源码及Class&web.xml下载:http://onpm.net/im/IPFilter.rar