2008-05-30

通过Struts的RequestProcessor做权限控制

关键字: requestprocessor, 权限

Struts是由中心控制器ActionServlet去处理请求地,

 中心控制器ActionServlet

 public void doPost(process(HttpServletRequest request, HttpServletResponse response)
{
   process(request,response);
}
protected void process(HttpServletRequest request, HttpServletResponse response)
 throws IOException, ServletException {
    ModuleUtils.getInstance().selectModule(request, getServletContext());
    ModuleConfig config = getModuleConfig(request);
    RequestProcessor processor = getProcessorForModule(config);
  //获取模块的请求处理器
    if (processor == null) {
 processor = getRequestProcessor(config);
    }
    processor.process(request, response);  //调用请求处理器处理请求
}

 

 

 请求处理器RequestProcessor

public void process(HttpServletRequest request, HttpServletResponse response)
 throws IOException, ServletException {
   。。。。。

   //读取请求路径对应的action-mapping信息

   //如果配置有form,则创建表单对象,并将表单信息填充到form bean中

  //调用ActionForm的validate()方法验证表单数据合法性

   //获取请求路径对应的Action

   //调用Action的execute()方法处理请求,放回ActionForward对象

   //根据mapping信息,跳转到相应页面
}

 一般情况下,无需配置此类,当有额外业务需要在请求到达ACTION之前处理时可以配置自己特定的

RequestRrocess类 此类应继承自RequestRrocess类,并重写其process方法

 

Package eud.tsinghua.strutsDemo
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.RequestProcessor;

public class MyRequestProcess extends RequestProcessor{
 
public void process(HttpServletRequest request,
  HttpServletResponse response)
  throws IOException,ServletException{
 //此处可以在请求到达ACTION之前处理一些业务
 
   super.process(request, response) ;
 }

}

 

在Struts-config.xml中配置

<controller
  processorClass="com.MyRequestProcess"
  contentType="text/html;charset=gbk"
  nocache="true"
  locale="true"
 ></controller>

属  性    描  述

processorClass    指定自定义的RequestProcessor类的全名

BufferSize    指定用来下载所用的缓存大小。缺省是4096字节。

contentType    定义response文本类型,缺省是text/html

Debug    定义当前系统的除错级别,缺省是0

Locale    如果是true,则在用户的session中存放Locale对象,缺省为true

maxFileSize    指定下载文件最大的大小。缺省是250M

multipartClass    指定去代替org.apache.struts.upload.DiskMultipartRequestHandler类的类的全名。 Nocache    如果是true,则会关闭每个response的缓存功能。缺省是false

TempDir    指定上载文件所用的临时目录。缺省值由容器决定

 

我写的权限类(简化)

package edu.tsinghua.t01.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RequestProcessor;

public class MyProcessor extends RequestProcessor {

 public boolean processPreprocess(HttpServletRequest request,
   HttpServletResponse response) {
  String path = "";

  try {
   path = super.processPath(request, response);
   System.out.println("ActionMapping的path属性: "+path);
   
   ActionMapping mapping = (ActionMapping) moduleConfig.findActionConfig(path);
   System.out.println("ActionMapping属性: "+mapping);
   
   String clazz = mapping.getType();
   System.out.println("Action的类名: "+clazz);
   
   String method = mapping.getParameter();
   System.out.println("method = "+method);
   
   if(clazz.equals("edu.tsinghua.t01.action.loginAction")){
    return true;
   }else{
    request.setAttribute("editError","不好意思,您的权限不足或该权限已被禁用!不能操作");
    request.getRequestDispatcher("/error.jsp").forward(request, response);
    return false;
   }
   
  }catch(Exception e){
   e.printStackTrace();
  }
  
  return false;
 }
}


 

评论
发表评论

您还没有登录,请登录后发表评论

cyh_it
搜索本博客
最近加入圈子
最新评论