禁止IP段访问页面

来源:百度知道 编辑:UC知道 时间:2024/05/30 13:46:58
我想只允许172.16.6的和服务器自身所在网段(172.17.10)访问某页面,现在我从网上DOWN了段代码下来,改怎么改,在确认正确后重定向委/login.jsp的文件,是JSP的文件

<%
String strRemoteIP = request.getRemoteAddr();
if (!strRemoteIP.equals("127.0.0.1"))
{
String tmpStr = strRemoteIP.substring(0,10);
if(!tmpStr.equals("172.18.70."))
{
//返回
//return;
//或者重定向到某个地方
response.sendRedirect("mailmain?type=init");
}
}
%>

public static boolean test(String strRemoteIP ){
Pattern p=Pattern.compile("^172\\.17\\.10");
Matcher m=p.matcher(strRemoteIP );
if(m.find()){return true;}
p=p.compile("^172\\.16\\.6");
m=p.matcher(strRemoteIP);
if(m.find())return true;
return false;
}
//在页面上要是不能这么来的话··你就讲上面的函数写在一个特定的类里
//然后再页面上调用。
//例如类是util,那么if(util.test(""));
String strRemoteIP = request.getRemoteAddr();
if(test(strRemoteIP ))//去你想去的地方