Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

struts拦截器

$
0
0

 在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";
}
}




}
}

作者:amzkqi 发表于2013-9-19 13:02:32 原文链接
阅读:78 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>