随笔-59  评论-31  文章-0  trackbacks-0
1.自定义拦截器继承AbstractInterceptor,重写public String intercept(ActionInvocation invocation)方法。
intercept方法有ActionInvocation对象,可以获取当前的Action请求。
public class AuthorityInterceptor extends AbstractInterceptor {
    private static final long serialVersionUID = 1L; 
    private Logger LOG = Logger.getLogger(AuthorityInterceptor.class.getName()); 
    
    private AuthorityUtil authorityUtil;
    
    public String intercept(ActionInvocation invocation) throws Exception {
        if (authorityUtil == null) {
            authorityUtil = new AuthorityUtil();
        }
        
        //获取当前用户所有的权限
        List<OperatorPurviewDO> operatorPurviews = getCurrentOperatorPurviews();
        
        //获取当前操作的url
        String currentUrl = getCurrentUrl(); 
        
         //如果是超级管理员或有当前url的权限,那么直接返回。
        if (OperatorUtil.getIsSuperAdmin() ||(OperatorUtil.getLoginName()!=null&&authorityUtil.checkUrl(operatorPurviews, currentUrl))){
             return invocation.invoke();
        }
         
        if (!OperatorUtil.getIsSuperAdmin()&&operatorPurviews.size()==0) {
            LOG.info("此用户:" + OperatorUtil.getLoginName() + " 没有任何角色,没有权限执行任何功能"); 
            return "loginErr"; 
        }   
            return "authorityErr";
    }

2.struts2.xml 配置interceptor

  2.1 定义自定义拦截器
<interceptor name="authorityInterceptor" class="com.wasu.eis.authority.AuthorityInterceptor" /> 
  2.2 加上struts2默认拦截器,形成拦截器栈
            <interceptor-stack name="eisManagerBasicStack">
                <interceptor-ref name="exception"/>
                <interceptor-ref name="alias"/>
                <interceptor-ref name="servletConfig"/>
                <interceptor-ref name="prepare"/>
                <interceptor-ref name="i18n"/>
                <interceptor-ref name="chain"/>
                <interceptor-ref name="debugging"/>
                <interceptor-ref name="profiling"/>
                <interceptor-ref name="scopedModelDriven"/>
                <interceptor-ref name="modelDriven"/>
                <interceptor-ref name="checkbox"/>
                <interceptor-ref name="staticParams"/>
                <interceptor-ref name ="fileUploadStack" /> 
                <interceptor-ref name="params">
                  <param name="excludeParams">dojo\..*</param>
                </interceptor-ref>
                <interceptor-ref name="conversionError"/>
                <interceptor-ref name="validation">
                    <param name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>
                <interceptor-ref name="workflow">
                    <param name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>
            </interceptor-stack>
            
            <interceptor-stack name="authorityInterceptorStack">
                <interceptor-ref name="authorityInterceptor" />
                <interceptor-ref name="eisManagerBasicStack" />
            </interceptor-stack>

3.设置为缺省的拦截器

<default-interceptor-ref name="authorityInterceptorStack"/>
posted on 2012-01-17 16:35 RoyPayne 阅读(2743) 评论(0)  编辑  收藏 所属分类: SSH

只有注册用户登录后才能发表评论。


网站导航: