日期:2014-05-18 浏览次数:20757 次
package com.ucm.interceptor;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.ucm.util.StringConstants;
import com.ucm.util.StringUtil;
/**
*
* 登录校验的interceptor
*
* @author Administrator
*/
public class UserAuthInterceptor extends MethodFilterInterceptor {
/**
*
*/
private static final long serialVersionUID = 1L;
protected Set<String> excludeMethods = Collections.emptySet();
//
// @Override
// public String intercept(ActionInvocation arg0) throws Exception {
// if (applyInterceptor(arg0)) {
// return doIntercept(arg0);
// }
// return arg0.invoke();
//
// }
@Override
protected String doIntercept(ActionInvocation arg0) throws Exception {
// 取得请求相关的ActionContext实例
ActionContext ctx = arg0.getInvocationContext();
Map session = ctx.getSession();
Object userId = session.get(StringConstants.SESSION_USER_ID);
// 如果没有登陆,都返回重新登陆
if (!StringUtil.isEmpty(userId)) {
return arg0.invoke();
}
ctx.put("tip", "你还没有登录");
return StringConstants.DEFAULT_PAGE;
}
public Set<String> getExcludeMethods() {
return excludeMethods;
}
public void setExcludeMethods(Set<String> excludeMethods) {
this.excludeMethods = excludeMethods;
}
}
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="login" extends="struts-default" namespace="/sysAdmin">
<!-- 拦截器 -->
<interceptors>
<!-- 自定义拦截器-->
<interceptor name="adminLogin" class="com.ucm.interceptor.UserAuthInterceptor">