Struts2 interceptor使用经验小结

1. interceptor 调用Spring容器中的bean

  在interceptor中常有需要调用Spring Bean的需要,其实很简单和Struts2的Action一样配置即可.

Spring中的配置

<!--spring配置 -->
1
<bean id="authorityInterceptor" class="com.xxx.interceptor.AuthorityInterceptor"/> 2 3 <bean id="operationInterceptor" class="com.xxx.interceptor.OperationInterceptor"> 4 <property name="defectService" ref="sysDefectService"/> 5 <property name="projectService" ref="projectService" /> 6 <property name="includeMethods"> 7 <value>*Modify,*Delete</value> 8 </property> 9 </bean>

Struts2中的配置

1      <interceptors>
2             <interceptor name="loginInterceptor" class="authorityInterceptor"/>
3             <interceptor name="operationInterceptor" class="operationInterceptor"/> 
5         </interceptors>

2. 如何获得当前Action名字  

public String intercept(ActionInvocation aInvocation) throws Exception {
  // 获取请求的action名称
  String actionName = aInvocation.getInvocationContext().getName();
  
  //获取参数集合
  Map parameters = aInvocation.getInvocationContext().getParameters();
 
   .... }

3. 方法拦截器黑白名单可以使用通配符

    拦截器代码:

  

package xx.interceptor;
import xx.action.ProjectAction;
import xx.action.SysDefectAction;
import xx.po.Project;
import xx.po.SysDefect;
import xx.po.User;
import xx.service.IProjectService;
import xx.service.ISysDefectService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import java.util.List;


public class OperationInterceptor extends MethodFilterInterceptor {
    private ISysDefectService defectService;
    private IProjectService projectService;
  
    protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
        boolean hasAuth = false;
        String cunrrentId = "";
        Object currentActon =actionInvocation.getAction();
        ActionContext actionContext = actionInvocation.getInvocationContext();
        User currentUser = (User)actionContext.getSession().get("currentUser");
        if(currentUser == null
                || currentUser.getEmployer()==null
                || currentUser.getGroup() == null
                || currentUser.getEmployer().getEmpId()+""==null
        )
        {
            return Action.LOGIN;
        }
        //获取当前用户的epmId
        String empId = currentUser.getEmployer().getEmpId() + "";

        if(currentActon instanceof ProjectAction){
            //是否第二次检查权限
            List<Project> projectList = currentUser.getProjectList();
            if (projectList==null || projectList.size()<1){
                ProjectAction projectAction = (ProjectAction)currentActon;
                cunrrentId = projectAction.getProjId();
                projectList =  projectService.getProjectsByEmpId(empId);
            }

            //如果获取列表失败,则提示无权限
            if(projectList==null || projectList.size()<1){
                return "deny";
            }else {
                currentUser.setProjectList(projectList);
            }
            for(Project project:projectList){
                if(cunrrentId.equals(project.getProjId()+"")){
                    hasAuth = true;
                }
            }
            if(hasAuth){
                return actionInvocation.invoke();
            }

        }else if(currentActon instanceof SysDefectAction){
            SysDefectAction sysDefectAction = (SysDefectAction)currentActon;
            List<SysDefect> sysDefectList =  defectService.getSysDefectsByEmpId(empId);
            if(sysDefectList==null || sysDefectList.size()<1){
                return "deny";
            }else {
                currentUser.setSysDefectList(sysDefectList);
            }
            for(SysDefect sysDefect:sysDefectList){
                if(cunrrentId.equals(sysDefect.getDefId()+"")){
                    hasAuth = true;
} }
if(hasAuth){ return actionInvocation.invoke(); } } return "deny"; //To change body of implemented methods use File | Settings | File Templates. } public ISysDefectService getDefectService() { return defectService; } public void setDefectService(ISysDefectService defectService) { this.defectService = defectService; } public IProjectService getProjectService() { return projectService; } public void setProjectService(IProjectService projectService) { this.projectService = projectService; } }

  Spring配置:    

    <bean id="authorityInterceptor" class="xx.interceptor.AuthorityInterceptor"/>

    <bean id="operationInterceptor" class="xx.interceptor.OperationInterceptor">
        <property name="defectService" ref="sysDefectService"/>
        <property name="projectService" ref="projectService" />
     <!-- 白名单属性配置,注意*的用法 -->
        <property name="includeMethods">
            <value>*Modify,*Delete</value>
        </property>
    </bean>

 

  struts2配置:

     <interceptors>
            <interceptor name="loginInterceptor" class="authorityInterceptor"/>
            <interceptor name="operationInterceptor" class="operationInterceptor"/>
            
            <interceptor-stack name="authInterceptor-stack">
                <interceptor-ref name="defaultStack"/>
                <interceptor-ref name="loginInterceptor"/>
                <interceptor-ref name="operationInterceptor"/>


            </interceptor-stack>

        </interceptors>

 

 

 

  

转载于:https://www.cnblogs.com/onmyway20xx/p/4014199.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值