struts1的权限控制

为了简便起见,我就做了个对是否登录的权限判断,原理都是一样,其他的可以类推。代码如下:

一、首先,自定义一个注解类,把该注解放在你想要拦截的action的方法上,至于到底是方法,通过下面的自定义的拦截器,根据反射技术获得。

Permission.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/*自定义注解:Permission*/
@Retention(RetentionPolicy.RUNTIME)  //注解执行的时机:程序运行时
@Target({ElementType.FIELD, ElementType.METHOD}) //注解应用的范围:属性、方法
public @interface Permission {
 public String name() default "";
}

二、然后,自定义一个拦截器,对所有的action进行拦截,判断是否有权限。该类继承了RequestProcessor类,这个类正是struts1拦截所有action的工作类,所以自定义的类事实上是重写了struts1的RequestProcessor类的处理action的方法。工作原理就是:根据自定义的注解,通过反射,获得要拦截的方法。对其进行权限判断。

MyInterceptorAction.java

import java.io.IOException;
import java.lang.reflect.Method;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RequestProcessor;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.actions.MappingDispatchAction;

public class MyInterceptorAction extends RequestProcessor {

 @Override
 protected ActionForward processActionPerform(HttpServletRequest request,
   HttpServletResponse response, Action action, ActionForm form,
   ActionMapping mapping) throws IOException, ServletException {
  // 得到struts想调用的方法  action===》execute   dispatchAction====>method--add
  Method method=getInvokeMethod(request, response, action, form, mapping);
  
  //检查方法上是否有注解
  Permission p=method.getAnnotation(Permission.class);
  
  if(p==null){//直接放行
   return super.processActionPerform(request, response, action, form, mapping);
  }else{
   //检查用户是否有权限
   User user=(User) request.getSession().getAttribute("user");
   if(user==null){
    request.setAttribute("errormessage", "请登录后再访问");
    return mapping.findForward("failedlogin");
   }
   //TODO 其他你要想进行权限判断的代码
  }
  
  return super.processActionPerform(request, response, action, form, mapping);
 }

 private Method getInvokeMethod(HttpServletRequest request,
   HttpServletResponse response, Action action, ActionForm form,
   ActionMapping mapping) {
  String methodName=null;
  if(DispatchAction.class.isAssignableFrom(action.getClass())){ //判断当前的action是否继承DispatchAction
   methodName=request.getParameter(mapping.getParameter()); //得到继承于DispatchAction的action执行的方法的名称
  }else if(MappingDispatchAction.class.isAssignableFrom(action.getClass())){
   methodName=mapping.getParameter(); //得到继承于MappingDispatchAction的action执行的方法的名称
  }else{
   methodName="execute";//得到继承于Action的action执行的方法的名称
  }
  Class<?>[] parameterTypes={ActionMapping.class,ActionForm.class,HttpServletRequest.class,HttpServletResponse.class};
  try {
   Method method=action.getClass().getMethod(methodName, parameterTypes);
   return method;
  } catch (Exception e) {
   throw new RuntimeException(e);
  }  
 }
}

第三、最后,别忘了配置struts-config.xml文件,主要是将自定义的拦截器让struts能够识别:

<controller processorClass="包名.MyInterceptorAction"/>

好了,就这么简单,最后,在你想拦截的action的方法上加上自定义的注解吧,@Permission ,然后要实现什么样的权限,就在第二步自定义的拦截器中的对应的位置写代码就OK了!

转载于:https://my.oschina.net/wangrikui/blog/192273

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值