如何过滤非法IP地址的用户发送请求
2007-06-03 00:33
267 查看
实现步骤:
1. 创建自己的RequestProcessor类,重写体重的proccessPreprocess加入所需的控制逻辑.源代码如下:
package classmate;
注意导包: commons-logging.jar
2. struts-config.xml文件配置如下:
加入:
<controller processorClass="classmate.Myrequ"/>
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.RequestProcessor;
import org.apache.struts.config.ForwardConfig;
import org.apache.commons.logging.Log;
/**
* 繼承RequestProcessor類
* @author java
*
*/
public class MyRequestProcessor extends RequestProcessor {
/**
* 無參數的構造方法
*
*/
public MyRequestProcessor() {
}
/**
* 重寫processpreprocess()方法
*/
protected boolean processPreprocess(HttpServletRequest request,
HttpServletResponse response) {
boolean continueProcessing = true;
//獲取發出請求的的客戶端IP地址
// Get the name of the remote host and log it
String remoteHost = request.getRemoteHost();
log.info("Request from host: " + remoteHost);
//判斷地址的合法性
// Make sure the host is from one that you expect
if ((remoteHost == null || !remoteHost.startsWith("127."))) {
// Not the localhost, so don't allow the host to access the site
continueProcessing = false;
try {
//跳轉到錯誤頁面
response.sendRedirect("/S02_Extend/error.jsp");
} catch (Exception ex) {
log.error("Problem sending redirect from processPreprocess()");
}
}
return continueProcessing;
}
}
1. 创建自己的RequestProcessor类,重写体重的proccessPreprocess加入所需的控制逻辑.源代码如下:
package classmate;
注意导包: commons-logging.jar
2. struts-config.xml文件配置如下:
加入:
<controller processorClass="classmate.Myrequ"/>
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.RequestProcessor;
import org.apache.struts.config.ForwardConfig;
import org.apache.commons.logging.Log;
/**
* 繼承RequestProcessor類
* @author java
*
*/
public class MyRequestProcessor extends RequestProcessor {
/**
* 無參數的構造方法
*
*/
public MyRequestProcessor() {
}
/**
* 重寫processpreprocess()方法
*/
protected boolean processPreprocess(HttpServletRequest request,
HttpServletResponse response) {
boolean continueProcessing = true;
//獲取發出請求的的客戶端IP地址
// Get the name of the remote host and log it
String remoteHost = request.getRemoteHost();
log.info("Request from host: " + remoteHost);
//判斷地址的合法性
// Make sure the host is from one that you expect
if ((remoteHost == null || !remoteHost.startsWith("127."))) {
// Not the localhost, so don't allow the host to access the site
continueProcessing = false;
try {
//跳轉到錯誤頁面
response.sendRedirect("/S02_Extend/error.jsp");
} catch (Exception ex) {
log.error("Problem sending redirect from processPreprocess()");
}
}
return continueProcessing;
}
}
相关文章推荐
- 如何拒绝非法IP地址的用户发送请求?
- linux服务器如何检查远程用户登陆状态、时间、ip地址等信息
- 如何配置路由器实现L2TP拨号用户始终使用指定的IP地址
- linux服务器如何检查远程用户登陆状态、时间、ip地址等信息
- 前端发送post请求,如何跟后端交互http://www.cnblogs.com/huhuuu/p/3993019.html
- 一个发送邮件功能,用户反映没有收到邮件,如何处理bug
- spring boot 过滤请求并发送到controller
- HttpClient使用SESSIONID模拟用户发送请求
- 在文件过滤驱动中如何判断文件访问请求来自网络?
- 如何使用火狐下的两款接口测试工具RESTClient和HttpRequester发送post请求
- 浏览器发送总共下载文件2第二个请求,如何“下载”仅仅记录1次要?
- 过滤用户表单非法字符
- [Jsoup] 如何发送Json请求(how to send json by Jsoup)
- linux服务器如何检查远程用户登陆状态、时间、ip地址等信息
- 如何使用SOCKET 发送HTTP1.1 GET POST请求包
- 如何使用SOCKET 发送HTTP1.1 GET POST请求包
- 如何使用POST man模拟发送一个需要cookie认证的请求
- 如何限制部分Notes用户接收发送Internet邮件
- 如何获取用户客户端的IP地址
- 如何使用SOCKET 发送HTTP1.1 GET POST请求包