基于struts的权限控制——简单实现

struts框架中只有一个ActionServlet,但可以有多个客户化的requestProcesscor。对于struts来说真正做控制的是RequestProecssor和Action。

其中Action的process方法最终调用的是RequestProcessor类中的process方法。

下面是该方法的源码:

public void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// 如果HTTP请求方式为post,并且contentType为”multipart/form-data”开头,标准的HttpServletRequest对象将被重新包装,
// 以方便处理”multipart”类型的HTTP请求如果请求方式为get,或正congtentType属性不是”mulitipart”,就直接返回原始的HttpServletRequest对象.
request = processMultipart(request);
// 获得请求的URI的路径,这一信息可用于选择合适的Struts Action组件.
String path = processPath(request, response);
if (path == null) {
return;
}
if (log.isDebugEnabled()) {
log.debug("Processing a '" + request.getMethod() +
"' for path '" + path + "'");
}
// 当ControllerConfig对象的locale属性为true,将读取用户请求中包含的Locale信息,然后把Locale实例保存在session范围内.
processLocale(request, response);
// 读取ControllerConfig对象的conttentType属性,
// 然后调用response.setContentType(contentType)方法,设置响应结果的文档类型和字符编码.
processContent(request, response);
// 读取ControllerConfig对象的nocache属性,
// 如果nocache属性为true,在响应结果中将加入特定的头参数:Pragma,Cache-Control和Expires
processNoCache(request, response);
// 该方法不执行任何操作.直接返回true.子类可以覆盖这个方法.
// 执行客户化的预处理请求操作.
if (!processPreprocess(request, response)) {
return;
}
this.processCachedMessages(request, response);
// 寻找和用户请求的URI匹配的ActionMapping,如果不存在这样的ActionMapping,则向用户返回恰当的错误信息
ActionMapping mapping = processMapping(request, response, path);
if (mapping == null) {
return;
}
// 先判断是否为Action配置了安全角色,如果配置了安全角色,就调用isUserInRole()方法判断当前用户是否具备必需的角色,
// 如果不具备,就结束请求处理流程.,向用户返回恰当的错误消息
if (!processRoles(request, response, mapping)) {
return;
}
// 先判断是否为ActionMapping配置了ActionForm,如果配置了ActionForm,就先从ActionForm的存在范围内(request或session)寻找改ActionForm实例,
// 如果不存在,就创建一个实例,接下来把它保存在合适的范围内,保存时使用的属性key为ActionMapping的name属性。
ActionForm form = processActionForm(request, response, mapping);
// 如果为ActionMapping配置了ActionForm,就先调用ActionForm的reset()方法,再把请求中的表单数据组装到ActionForm中
processPopulate(request, response, form, mapping);
// 如果为ActionMapping配置了ActionForm,并且ActionMapping的validate属性为true,
// 就调用ActionForm的validate()方法,如果validate方法返回的ActionErrors对象中包含ActionMessage对象,
// 说明表单验证失败。就把ActionErrors对象放在request范围内,再把请求转发到ActionMapping的input属性指定的Web组件。
// 如果ActionForm的validate方法执行表单验证成功,就继续执行下面的处理流程。
try {
if (!processValidate(request, response, form, mapping)) {
return;
}
} catch (InvalidCancelException e) {
ActionForward forward = processException(request, response, e, form, mapping);
processForwardConfig(request, response, forward);
return;
} catch (IOException e) {
throw e;
} catch (ServletException e) {
throw e;
}
// Process a forward or include specified by this mapping
if (!processForward(request, response, mapping)) {
return;
}
if (!processInclude(request, response, mapping)) {
return;
}
// 先判断是否在Action缓存中存在这个Action实例,如果没有就新建一个Action实例,
// 把它放在Action缓存中。可以看出Action也是只有一个实例在运行的
Action action = processActionCreate(request, response, mapping);
if (action == null) {
return;
}
// Call the Action instance itself
ActionForward forward =
processActionPerform(request, response,
action, form, mapping);
// 把你的Action的excute方法返回的ActionFoward对象作为参数传给它,
// processActionForward对象包的请求转发信息来执行请求转发或重定向。
processForwardConfig(request, response, forward);

}

下面是我自己写的一个实际使用的类,对于一般的小型系统的权限控制个人感觉够用:

package com.shy.test; import java.util.ArrayList; import java.util.Iterator; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.RequestProcessor; /** * @author shy.qiu * struts配置文件配置方法 * <controller * contentType="text/html;charset=utf-8" * locale="true" * nocache="true" * processorClass="com.shy.web.ShyRequestProcessor"> * </controller> */ public class ShyRequestProcessor extends RequestProcessor { // 普通用户的权限数组 ArrayList<String> authority1 = new ArrayList<String>(); // 管理员的权限数组 ArrayList<String> authority2 = new ArrayList<String>(); public ShyRequestProcessor(){ //建议从数据库中读取,并放入session中 // 普通用户权限组装 authority1.add("TSKR0010"); authority1.add("TSKR0210"); authority1.add("TSKR0220"); authority1.add("TSKR0230"); // 管理员权限组装 authority2.add("TSKR0310"); authority2.add("TSKR0320"); authority2.add("TSKR0330"); authority2.add("TSKR0340"); authority2.add("TSKR0350"); authority2.add("TSKR0360"); authority2.add("TSKR0370"); authority2.add("TSKR0380"); } protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) { // 通过标记位 boolean flag=true; // 权限名称 String kengen=""; // 请求路径 String action_path=""; try { // 获得请求的URI的路径,这一信息可用于选择合适的Struts Action组件. action_path = super.processPath(request, response); // 登陆权限全都返回true if ("login".equals(action_path)) { flag = true; } else { // 得到权限名称 if(request.getSession().getAttribute("kengen")!=null) { kengen = request.getSession().getAttribute("kengen").toString(); // 没有任何权限 } else { request.setAttribute("Error", "你没有该权限"); request.getRequestDispatcher("/index.jsp").forward(request,response); return false; } // 当用户为管理员时 if ("2".equals(kengen)) { for (Iterator<String> it = authority2.iterator();it.hasNext();) { String s_authority = (String)it.next(); if(action_path.equals(s_authority)){ flag = true; } } } // 当用户为普通用户时 else if ("1".equals(kengen)) { for (Iterator<String> it = authority1.iterator();it.hasNext();) { String s_authority = (String)it.next(); if(action_path.equals(s_authority)){ flag = true; } } } } // 权限通过标记为为false时 if(!flag) { request.setAttribute("Error", "你没有该权限"); request.getRequestDispatcher("/index.jsp").forward(request,response); return false; } // 权限通过标记为为true时 else { return true; } } catch (Exception e) { e.printStackTrace(); return false; } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值