哪位大哥有关于struts2登陆拦截的代码?示例

解决方案 »

  1.   

    拦截器说白了就是一个filter  拦截器链就是一堆的filter
      

  2.   


    import org.apache.struts2.ServletActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
    import com.gbsou.dns.common.Constants;/**
     * @author Jeson
     * @blog http://www.gbsou.com
     * @date:Oct 30, 2009 1:51:34 PM
     * @version :1.0
     * 
     */
    public class LoginInterceptor extends MethodFilterInterceptor{
    private String exIncludeMethod = null;
    @Override
    protected String doIntercept(ActionInvocation inv)
    throws Exception {
    String method = inv.getInvocationContext().getName();
    if ("saveLog".equals(method)) {
    return inv.invoke();
    }

    if (!isIgnoreMethod(method)) {
    Object isLogin = ServletActionContext.getRequest().getSession()
    .getAttribute(Constants.SESSION_USER_IS_LOGIN);
    if (isLogin == null || !"y".equals(isLogin)) {
    ServletActionContext
    .getRequest()
    .setAttribute(Constants.ERROR_INFO,
    "用户信息丢失,请重新登陆 <a href='"+ServletActionContext.getServletContext().getAttribute(Constants.WEBAPP_CONTEXT_PATH_KEY)+"/login/t_memLogin.action'>登录</a>");
    throw new NullPointerException("用户信息丢失");
    }
    }
    return inv.invoke();
    } private boolean isIgnoreMethod(String targetActionName) {
    boolean flag = false;
    targetActionName = targetActionName.toLowerCase();
    String[] Methods = exIncludeMethod.split(",");
    for (String m : Methods) {
    if (targetActionName.indexOf(m) != -1) {
    flag = true;
    break;
    }
    }
    return flag;
    } public String getExIncludeMethod() {
    return exIncludeMethod;
    } public void setExIncludeMethod(String exIncludeMethod) {
    this.exIncludeMethod = exIncludeMethod;
    }
    }