struts2 攔截器驗證權限

struts2 攔截器驗證權限
1.在action調用前攔截
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthorityInterceptor extends AbstractInterceptor {

private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation arg0) throws Exception {
//action 名稱
String actionName=arg0.getInvocationContext().getName();
System.out.println("action name:"+actionName);
//action 參數
//Map parameters=arg0.getInvocationContext().getParameters();
         ActionContext context = arg0.getInvocationContext();
         Map<String, Object> session = context.getSession();
         String flag =(String) session.get("flag");        
         if(null == flag){
         System.out.println("session驗證失敗");
             return "login";
         }
                //驗證權限       
         if(CheckLimit(action))
return arg0.invoke();
     System.out.println("權限驗證失敗");
       return "quanxian";
        
     }

}
2.配置struts.xml文件
<interceptors>
            <interceptor  class="com.sunny.filter.AuthorityInterceptor" name="authority"/> 
            <interceptor-stack name="mydefault">
                <interceptor-ref name="defaultStack"></interceptor-ref>
                <interceptor-ref name="authority"></interceptor-ref>
            </interceptor-stack>
        </interceptors>
<default-interceptor-ref name="mydefault"/>
發佈了177 篇原創文章 · 獲贊 3 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章