📄 authenticationinterceptor.java
字号:
package com.sunwah.baseapp.interceptor;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.sunwah.baseapp.common.Constants;
import com.sunwah.baseapp.system.vo.UserVO;
/**
*
* 权限拦截器
*
* @author MARK
*
*/
public class AuthenticationInterceptor implements Interceptor {
/**
*
*/
private static final long serialVersionUID = -5458936346743520250L;
private static Logger logger = Logger
.getLogger(AuthenticationInterceptor.class.getName());
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation actionInvocation) throws Exception {
// 获取会话对象的应用
Map session = actionInvocation.getInvocationContext().getSession();
Object obj = session.get(Constants.SESSION_USER);
// 判断是否登录
if (obj == null) {
return Action.LOGIN;
} else {
UserVO user = (UserVO) obj;
// 从会话中获取用户拥有的资源列表
List<String> resourceList = user.getResourceList();
// 同时判断actionName和method是为了兼容Struts2 ACTION的不同配置
String actionName = actionInvocation.getProxy().getActionName();
String methodName = actionInvocation.getProxy().getMethod();
String resource = actionName.equals(methodName) ? actionName
: actionName + "!" + methodName;
logger.debug(resource);
// 判断请求动作是否在用户拥有的资源列表中
if (!resourceList.contains(resource))
return "auth_error";
}
// 执行具体的请求动作
return actionInvocation.invoke();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -