如何过滤非法IP地址的用户发送请求

实现步骤: 

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;
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值