隨筆-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() + " 沒有任何角色,沒有權限執(zhí)行任何功能"); 
                      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 閱讀(2765) 評論(0)  編輯  收藏 所屬分類: SSH
          主站蜘蛛池模板: 保定市| 滕州市| 乾安县| 公安县| 宜兰市| 陆丰市| 台中县| 林芝县| 武川县| 裕民县| 阳原县| 武城县| 固始县| 临武县| 太原市| 公安县| 亳州市| 长治县| 苍梧县| 马公市| 乐亭县| 滦平县| 广水市| 永吉县| 鄯善县| 彭山县| 大竹县| 威远县| 德兴市| 陇西县| 郁南县| 金华市| 海原县| 望都县| 会昌县| 宝兴县| 潮安县| 扶绥县| 大邑县| 台北市| 诸暨市|