隨筆-59  評(píng)論-31  文章-0  trackbacks-0
          1.自定義攔截器繼承AbstractInterceptor,重寫public String intercept(ActionInvocation invocation)方法。
          intercept方法有ActionInvocation對(duì)象,可以獲取當(dāng)前的Action請(qǐng)求。
          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();
                  }
                  
                  //獲取當(dāng)前用戶所有的權(quán)限
                  List<OperatorPurviewDO> operatorPurviews = getCurrentOperatorPurviews();
                  
                  //獲取當(dāng)前操作的url
                  String currentUrl = getCurrentUrl(); 
                  
                   //如果是超級(jí)管理員或有當(dāng)前url的權(quán)限,那么直接返回。
                  if (OperatorUtil.getIsSuperAdmin() ||(OperatorUtil.getLoginName()!=null&&authorityUtil.checkUrl(operatorPurviews, currentUrl))){
                       return invocation.invoke();
                  }
                   
                  if (!OperatorUtil.getIsSuperAdmin()&&operatorPurviews.size()==0) {
                      LOG.info("此用戶:" + OperatorUtil.getLoginName() + " 沒有任何角色,沒有權(quán)限執(zhí)行任何功能"); 
                      return "loginErr"; 
                  }   
                      return "authorityErr";
              }

          2.struts2.xml 配置interceptor

            2.1 定義自定義攔截器
          <interceptor name="authorityInterceptor" class="com.wasu.eis.authority.AuthorityInterceptor" /> 
            2.2 加上struts2默認(rèn)攔截器,形成攔截器棧
                      <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.設(shè)置為缺省的攔截器

          <default-interceptor-ref name="authorityInterceptorStack"/>
          posted on 2012-01-17 16:35 RoyPayne 閱讀(2759) 評(píng)論(0)  編輯  收藏 所屬分類: SSH
          主站蜘蛛池模板: 镇赉县| 普兰店市| 永福县| 宁海县| 旌德县| 罗城| 新巴尔虎左旗| 珲春市| 榆中县| 巧家县| 垫江县| 武城县| 克东县| 绵竹市| 济南市| 河北区| 弥渡县| 南充市| 集安市| 泸西县| 岳阳市| 庄河市| 东平县| 平山县| 沈丘县| 香格里拉县| 冷水江市| 那曲县| 盐山县| 奉新县| 德保县| 甘洛县| 毕节市| 鹤山市| 武胜县| 蒙山县| 寿宁县| 阿拉善右旗| 霸州市| 子洲县| 博白县|