問題的提出
在Struts
解決方法
巴巴運動網通過覆蓋DelegatingRequestProcessor控制器的processActionPerform方法
本方法解決思想
最終既可以攔截到execute方法
第一步
@Override
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
return super
}
//假如這個action中有一個具體的方法
@Privilege(userType=PrivilegeType
public ActionForward addUI(ActionMapping mapping
HttpServletRequest request
CategoryForm categoryForm = (CategoryForm)form;
categoryForm
return mapping
}
第二步
看上面addUI方法上的注解應該可以理解這些配置
@Retention(RetentionPolicy
@Target(ElementType
public @interface Privilege {
String message() default
PrivilegeType userType();
}
配置中有個字字段是枚舉
public enum PrivilegeType {
LoginUser{
public String getName(){
return
}
public int getValue(){
return
}
}
LoginUserSelf{
public String getName(){
return
}
public int getValue(){
return
}
}
Moderator{
public String getName(){
return
}
public int getValue(){
return
}
}
Admin{
public String getName(){
return
}
public int getValue(){
return
}
};
public abstract String getName();
public abstract int getValue();
}
第三步
@Aspect
@Component()
public class PrivilegeAction {
//攔截com
@Around(
public Object validatePrivilege(ProceedingJoinPoint pjp) throws Throwable{
// 從攔截的方法中參數中得到四個對象
ActionMapping mapping = (ActionMapping) pjp
ActionForm form = (ActionForm) pjp
HttpServletRequest request = (HttpServletRequest) pjp
HttpServletResponse response = (HttpServletResponse) pjp
//從攔截點處獲取它所處的類名
Class dispatchAction = Class
ActionMapping
ActionForm
HttpServletRequest
HttpServletResponse
//如果方法上沒有配置權限信息
if(privilege==null)
return (ActionForward)pjp
//否則必須是登錄用戶
User user = WebUtil
if(user==null){
request
return mapping
} //再根據用戶類型進行細粒度攔截
switch(privilege
//當權限為PrivilegeType
//己的文章才有權限更新)時
//戶匹配
case
if(!user
request
return mapping
}
break;
//版主權限攔截
case
request
return mapping
//管理員權限
case
String admin = ((SystemProperty)request
if(!user
request
return mapping
}
break;
}
//當上面沒有攔住
return (ActionForward)pjp
}
}
以上細粒度攔截屬業務邏輯
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28601.html