package com.app.common; import org.apache.log4j.Logger; import com.app.model.User; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class PrivilegeInterceptor extends AbstractInterceptor{ /** * */ private static final long serialVersionUID = 5851995979071627976L; private static final Logger logger = Logger.getLogger(PrivilegeInterceptor.class); @Override public String intercept(ActionInvocation invocation) throws Exception { logger.info("PrivilegeInterceptor:Action方法拦截"); //获取当前用户 User user = (User) ActionContext.getContext().getSession().get("loginUser"); //获取当前访问的URL,并去掉当前应用程序的前缀(也就是 namespaceName + actionName ) String namespace = invocation.getProxy().getNamespace(); String actionName = invocation.getProxy().getActionName(); logger.info("namespace:" + namespace +" , "+"actionName:" + actionName); String privilegeUrl = null; if(namespace.endsWith("/")){ privilegeUrl = namespace + actionName; }else{ privilegeUrl = namespace + "/" + actionName; } //要去掉开头的'/' if(privilegeUrl.startsWith("/")){ privilegeUrl = privilegeUrl.substring(1);//从1开始截取 } //String result = null; //如果未登录用户 if(user==null){ // if(privilegeUrl.endsWith("/login_index" + Constant.S2_ACTION_EXT) // || privilegeUrl.endsWith("/login_login" + Constant.S2_ACTION_EXT )){ // //如果是正在使用登录功能,就放行 // result = invocation.invoke(); // }else{ //如果不是去登录,就转到登录页面 return "login"; // } } //如果已经登录,就判断权限 else{ //if(user.hasPrivilegeByUrl(privilegeUrl)){ //如果有权限就放行 return invocation.invoke(); //}else{ //如果没有权限,返回无权限信息 // return "noPrivilegeError"; //} } //return result; } }