struts2 拦截器验证权限
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))
System.out.println("权限验证失败");
return "quanxian";
}
<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>
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();
return arg0.invoke();
}
2.配置struts.xml文件
<interceptors>
<default-interceptor-ref name="mydefault"/>