在struts.xml配置文件中添加注册
<interceptors>
<!-- 权限拦截器 --><interceptor name="checkPrivilege" class="com.demo.util.CheckPrivilegeInterceptor"></interceptor>
<!-- 定义了一个新的拦截器栈,其中包含defaultStack,
并覆盖了defaultStack中一个modelDriven拦截器的参数 -->
<interceptor-stack name="myStack">
<interceptor-ref name="checkPrivilege"></interceptor-ref>
<interceptor-ref name="defaultStack"><param name="modelDriven.refreshModelBeforeResult">true</param></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 指定默认使用myStack拦截器栈 -->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
/**
* 访问后台action经过权限拦截器
*/
@SuppressWarnings("serial")
public class CheckPrivilegeInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception{
Userinfo user =(Userinfo) ActionContext.getContext().getSession().get("user");
String url =invocation.getProxy().getActionName();
if (url.endsWith("UI")) {
url = url.substring(0, url.length() - 2);
}
url += ".action";
if(user ==null){
if(url.equals("userAction_login.action")||url.equals("userAction_logout.action")){
return invocation.invoke();
}else{
return "noPrivilegeError";
}
}else{
if (user.hasPrivilegeByUrl(url)) {
return invocation.invoke(); // 放行
} else {
return "noPrivilegeError";
}
}
}
}