隨筆-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 閱讀(2759) 評論(0)  編輯  收藏 所屬分類: SSH
          主站蜘蛛池模板: 陇南市| 海兴县| 潞西市| 神池县| 保山市| 双辽市| 南康市| 米林县| 潜山县| 宁德市| 乐安县| 舞钢市| 东台市| 彰武县| 五寨县| 财经| 重庆市| 郎溪县| 武安市| 犍为县| 昌黎县| 万州区| 西宁市| 万盛区| 鹤岗市| 民勤县| 当涂县| 彝良县| 普洱| 宿州市| 长沙县| 宜阳县| 罗定市| 汝城县| 道孚县| 湖北省| 胶南市| 虎林市| 马公市| 河池市| 武夷山市|