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 閱讀(2756)
評論(0) 編輯 收藏 所屬分類:
SSH