自定义拦截器(一)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package cn.itcast.crm.interceptor;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class extends AbstractInterceptor {
String excludUrls = "";
* 不拦截的url,具体的值是从Struts.xml中来
* @param urls
*/
public void setExcludUrls(String urls){
this.excludUrls=urls;
}
public String intercept(ActionInvocation invocation) throws Exception {
// 判断是否有当前登录人,从session获取对象,如果对象为null,代表没有登录人,跳转到登录页面,如果有对象,直接放过
Object user = ServletActionContext.getRequest().getSession().getAttribute("user");
if(user!=null){
invocation.invoke(); //直接放过
}
// methodUrl 当前运行的方法
String methodUrl = ServletActionContext.getContext().getName();
// excludUrl 不需要拦截的方法
String[] urls = excludUrls.split(",");
for (String url : urls) {
if(methodUrl.equals(url)){
invocation.invoke(); //直接放过
}
}
return "login";
}
}