拦截器可以与Filter过滤器集合起来使用,Interceptor的配置在struts.xml中配置,可以拦截指定的struts2请求,一般是指.action后缀的请求。
[java]
package edu.press.utils;
import javax.servlet.http.*;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.struts2.ServletActionContext;
/**
* Interceptors for security
* @author
* @version 1.0
*
*/
public class AdminInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
private HttpSession session;
public String intercept(ActionInvocation invocation) throws Exception {
session = ServletActionContext.getRequest().getSession(true);
// 简单的验证会话session是否有指定属性
if (session.getAttribute("XXXXXXXX") != null) {
return invocation.invoke(); //权限符合,请求顺利执行
}
return Action.LOGIN; //请求未通过,转向LOGIN的result
}
}
在struts.xml文件中配置:
[html]
下边这是我们项目中的定义,拦截了多有继承struts-defult的package里的Action
[ html]
对于需要拦截的action,可按照如下例子:
[html]
摘自 那年那月那天