1. interceptor 调用Spring容器中的bean

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

Spring中的配置

<!--spring配置 -->
1 <bean id="authorityInterceptor" class="com.xxx.interceptor.AuthorityInterceptor"/> <bean id="operationInterceptor" class="com.xxx.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"/>
</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>

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

  1. Struts2 Interceptor学习

    Interceptor的设计思想,其实是Spring里面的AOP思想,尽管Struts2又有自己的Interceptor但是,在实际开发中,用的较少,SSH整合之后你可以采用AOP事务处理进行拦截,更 ...

  2. Struts2 - Interceptor中取得ActionName、Namespace、Method

    在Struts2的Interceptor中取得当前执行对应的ActionName.Namespace.Method方法: 可以使用: System.out.println(invocation.get ...

  3. Struts2命令空间小结

    sturts2命名空间小结,以tomcat为服务器 1. 命名空间配置为“/” <package name="default" namespace="/" ...

  4. 【心得】Lattice后端使用经验小结(ECP5UM,DDR3,Diamond3.10,Reveal逻辑分析)

    [博客导航] [导航]FPGA相关 背景 下边的内容,适合初次使用Lattice的.具备FPGA开发经验的同学. 1.初次使用,还真遇到不少的坑,Lattice的工具也有不少优缺点,通过总结,希望能缩 ...

  5. Struts2(七)基础小结

    一.struts2和action 二.Result 三.struts.xml 四.namespace 第一种绝对路径 <form action="${pageContext.reque ...

  6. Git使用经验小结

    2012春,开始正式使用SVN,2014年9月加入一起好后,开始使用Git.  有了一些使用经验,也看了下网上关于"Git全胜SVN"的言论. 结合自己的实际情况,简要写几句: 1 ...

  7. mybatis 使用经验小结

    一.多数据源问题 主要思路是把dataSource.sqlSesstionFactory.MapperScannerConfigurer在配置中区分开,各Mapper对应的包名.类名区分开 <? ...

  8. jmeter linux使用经验小结

    1. 确认务必安装了JDK,并且把路径配置OK.否则执行会报错. 2. 当做负载机时,在hosts 配置上    你的ip   你的hostname  或者使用./bin/jmeter-server ...

  9. DEV_TreeList使用经验小结

    1. 点击叶子节点是希望Open键显示,点击非叶子节点时希望隐藏.实践中发现点击到了非叶子节点图标,Open没有隐藏,如何解决? 增加一个判断: if (_hitInfo.HitInfoType != ...

随机推荐

  1. 聊一聊 AngularJS 服务

    什么是服务? 在 AngularJS 中,服务是一个函数或对象,可在你的 AngularJS 应用中使用. AngularJS 内建了30 多个服务. 为什么使用服务? 在很多服务中,比如 $loca ...

  2. ###《High-level event recognition in unconstrained videos》

    Author: Yu-Gang Jiang, Shih-Fu Chang 事件检测的目标就是自动识别给定视频序列中的感兴趣事件.进行视频事件检测通常很困难,特别是在网络中非限制的视频.在非限制情况下, ...

  3. [.Net MVC] 用户角色权限管理_使用CLK.AspNet.Identity

    项目:后台管理平台 意义:一个完整的管理平台需要提供用户注册.登录等功能,以及认证和授权功能. 一.为何使用CLK.AspNet.Identity 首先简要说明所采取的权限控制方式.这里采用了基于角色 ...

  4. activemq spring 配置

    Apache ActiveMQ是最流行和最强大的开源消息集成模式服务器.Apache ActiveMQ是速度快,支持多跨语言的客户端和协议,带有易于使用企业集成模式和许多先进的功能在充分支持JMS 1 ...

  5. Java实战之02Hibernate-05检索策略、检索方式

    十一.Hibernate的检索策略 1.概述: 查询的时机:什么时候去查? /** * 一张表的检索策略我们称之为: * 类级别的检索策略. * 注意:只要是说类级别的检索策略,就一定不涉及关联对象. ...

  6. 导出Execel

    //请求入口 @SuppressWarnings("serial")@ParentPackage("default") //action请求@Results( ...

  7. bzoj 1096: [ZJOI2007]仓库建设

    dp是很好想的了,关键是数据太大,普通dp肯定超时,所以一定有用某种优化,dp优化也就那么几种,这道题用的是斜率优化,先写出普通的状态转移方程: dp[i] = min{  dp[j] + Σ ( p ...

  8. 重力加速度陀螺仪传感器MPU-6050(一)

    MPU-60X0 对陀螺仪和加速度计分别用了三个16 位的ADC,将其测量的模拟量转化 为可输出的数字量.为了精确跟踪快速和慢速的运动,传感器的测量范围都是用户可控的,陀螺仪可测范围为±250,±50 ...

  9. ViewState压缩

    /// <summary> ///CompressViewState 的摘要说明 /// </summary> public class CompressViewState:S ...

  10. C指针笔记

    指针的学习 两个数比较大小,通过传递内容进行比较 #include <stdio.h> void swap(int *p1, int *p2){ int temp; //注意指变量*的两个 ...