隨筆-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 閱讀(2765) 評論(0)  編輯  收藏 所屬分類: SSH
          主站蜘蛛池模板: 抚宁县| 琼中| 怀远县| 彰化县| 石河子市| 昌黎县| 湛江市| 赣榆县| 若尔盖县| 宽城| 新源县| 包头市| 临沭县| 绥阳县| 浠水县| 波密县| 成都市| 荔波县| 南昌市| 乌拉特前旗| 海南省| 遂川县| 湖州市| 玛曲县| 阿拉善右旗| 沽源县| 得荣县| 宁波市| 洛南县| 海盐县| 泸西县| 淄博市| 新闻| 辽阳县| 罗山县| 潞西市| 和林格尔县| 札达县| 尉犁县| 图片| 板桥市|