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 com.jd.jd_ptesting.interceptor;
import com.jd.jd_ptesting.action.ProjectAction;
import com.jd.jd_ptesting.action.SysDefectAction;
import com.jd.jd_ptesting.po.Project;
import com.jd.jd_ptesting.po.SysDefect;
import com.jd.jd_ptesting.po.User;
import com.jd.jd_ptesting.service.IProjectService;
import com.jd.jd_ptesting.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="com.jd.jd_ptesting.interceptor.AuthorityInterceptor"/>

    <bean id="operationInterceptor" class="com.jd.jd_ptesting.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>
时间: 2024-10-11 19:48:04

Struts2 interceptor使用经验小结的相关文章

Struts2 Interceptor学习

Interceptor的设计思想,其实是Spring里面的AOP思想,尽管Struts2又有自己的Interceptor但是,在实际开发中,用的较少,SSH整合之后你可以采用AOP事务处理进行拦截,更方便 ---------------------------------华丽的分割线--------------------------------------- 从一个简单的DEMO入手,正常情况下,客户端可以直接访问我的Action,但是我不想让他们访问,就在Struts2和Action之间架设

struts2中拦截器(interceptor)小结

什么是拦截器? java里的拦截器是动态拦截Action调用的对象.它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行,同时也提供了一种可以提取action中可重用部分的方式.在AOP(Aspect-Oriented Programming)中拦截器用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作.Struts2内置了很多拦截器,每个拦截器完成相对独立的功能,多个拦截器的组合体成为拦截器栈.最为重要的拦截器栈是

Struts2命令空间小结

sturts2命名空间小结,以tomcat为服务器 1. 命名空间配置为“/” <package name="default" namespace="/" extends="struts-default"> <action name="HelloWorld" class="com.venn.action.HelloWorldAction"> <result>/jsp/te

Clean Cache Struts2 Interceptor Tutorial

Page 1 of 2 The first tutorial concentrate on creating a basic interceptor, but useful in a typical web application. This is very common that developer wants to restrict the browser to  cache rendered pages. And the way we used to achieve is having c

Struts2 Interceptor Life Cycle

Page 1 of 2 In the last tutorial we did successfully create a working interceptor, but there is more to it. To successfully develop bug free interceptors we need to know more. In this tutorial will try to understand a crucial part of interceptor. And

Struts2(七)基础小结

一.struts2和action 二.Result 三.struts.xml 四.namespace 第一种绝对路径 <form action="${pageContext.request.contextPath }/user/login.action" method="post"> 第二种  <form action="<%=request.getContextPath() %>/user/login.action"

1、struts2漏洞利用小结

从今天开始,打算将自己的挖洞历程一点一滴给记录下来,从17年开始接触web渗透,学完了cracer17年的教程,看过网易公开课,目前在安全牛课堂学习kali渗透测试.心里其实很感慨,学了很多,感觉会得太少,到现在挖洞经验仍然为0 (想哭)T_T! 偶尔会很迷茫,自己到底学了了什么呢,学而不会有何用!!! 这是一个新的开始,以练代学,由浅入深,特以此系列献给和我曾经一样犯过迷茫的孩子. 闲话不说,进入正题吧,这些文章我会定期完善和补充,希望大家伙可以多多交流. 第一站,让我们来研究一下struts

mybatis 使用经验小结

一.多数据源问题 主要思路是把dataSource.sqlSesstionFactory.MapperScannerConfigurer在配置中区分开,各Mapper对应的包名.类名区分开 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="h

Kafka使用经验小结

本文尽量从一个使用者的角度去记录一些在实战当中使用Kfaka所需要关注的要点,这样可能会贴切更多的读者,本文并不会介绍太多的Kafka的一些架构层次设计的知识,因为网上已经有一大堆的重复搬运的资料任由你们学习参考. 明确Kafka在你的系统中的定位 众所周知,Kafka的可用性和数据可靠性相对其他的高可用的MQ来说会低一点,但是带来的却是更大更高性能的消息吞吐量的优势,因此要是你的系统需要的是金融级别的高可靠高可用就尽量选择其他的MQ产品. Kafka比较适合那种容忍即使丢失一定量数据也不会带来