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;}//获取当前用户的epmIdString 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使用经验小结相关推荐
- nest笔记十:typeorm使用经验小结
nest笔记十:typeorm使用经验小结 nestjs系列笔记 之前的笔记:nest记笔五:使用TypeORM连接mysql,可以使用typeorm的基本功能.随着开发的深入,很多已经满足不了要求了 ...
- UrlReWriter 使用经验小结
UrlRewriter 是微软封装好了的一个URL重写组件.使用它可以让我节约很多自已开发的时间. 好了,开始讲述我的应用经验,这只是很菜鸟的经验,高手就不用看了. 第一步,请从此下载此组件.解压,把 ...
- Kafka使用经验小结
本文尽量从一个使用者的角度去记录一些在实战当中使用Kfaka所需要关注的要点,这样可能会贴切更多的读者,本文并不会介绍太多的Kafka的一些架构层次设计的知识,因为网上已经有一大堆的重复搬运的资料任由 ...
- Superpro5000的使用经验小结
1.IC的插入方向 这种编程器的IC插入方向是: 1脚在图片的左上角,而且整个芯片的插入位置要靠近图片下方的小扳手: IC如果差错了,编程器上位机会报错: 另外上位机软件要选择对应的IC名称进行程序烧 ...
- Lucene使用经验小结
最近对公司一个使用Lucene的搜索服务进行了维护,踩了一些坑,总结了一点经验,在此做个记录 1.分享一个十分有用的视频教程 由于Lucene没有中文官方文档,英文API文档对新手来说看得十分痛苦,加 ...
- matlab乘号前加点,matlab使用经验小结
1.shift+enter是一次输入几条语句时换行用的:空格+...+enter是一句话分行时的接续! 2.通过what命令可显示出搜索路径上的文件名,如:what(显示当前目录中的文件):what ...
- struts2文件上传大小限制问题小结(引用)
最后解决办法: 页面js控制上传文件的大小,在页面进行控制.如下代码 inputs是所有文本上传input DOM //名称信息var nameStr='';//大小信息var sizeStr=''; ...
- struts2随笔(一)Action、struts.xml、Interceptor细节
Struts2由Struts1和webWork两个经典MVC框架发展起来.与传统的Struts1相比,Struts2允许使用不同的Java对象作为Action.支持更多的视图技术,基于AOP思想的拦截 ...
- Struts2学习笔记(八) 拦截器(Interceptor)(上)
什么是拦截器(Interceptor) 拦截器是struts2框架最重要的特性之一.它可以让你在Action 方法执行前后(Result执行之后)进行一些功能处理.在前面学习Action和学习Resu ...
最新文章
- PHP - 逗号和点号的区别
- JS作用域和作用域链
- 机器学习(Machine Learning)深入学习(Deep Learning)资料
- Unity3D脚本中文系列教程(五)
- 乘法逆元总结 3种基本方法
- 华为手机输入键盘声音_华为手机键盘隐藏7个秘密,你只用来打电话?来一起解锁新玩法...
- 从源码的角度说说Activity的setContentView的原理(二)
- 欲取代硬盘?SSD固态存储器前景分析
- 下滑加载更多js_vue.js怎么实现滑动到底部加载更多数据效果?
- MMO游戏数值框架概述(偏模拟方向)
- Netty 5 io.netty.util.IllegalReferenceCountException 异常
- 《3D数学基础》1.9 向量空间
- Constructing Narrative Event Evolutionary Graph for Script Event Prediction
- 复杂问题的知识问答技术介绍
- Python:NBA球员得分数据排行爬虫
- 开发脂肪秤方案PCBA设计
- 嵌入式操作系统--篮球记分计时系统
- 程序员的数学【多元函数微分学】
- VM2230 asdebug.js:xxx:端口不在以下 request 合法域名列表中
- 超赞!Win10自带系统修复工具DISM使用说明
热门文章
- @ResponseBody 和 @RequestBody 的作用
- 在Ubuntu上安装Odoo 11(企业版)
- Java的缓存使用-Ehcache
- 《高阶Perl》——导读
- SharePoint 2016 必备组件离线安装介绍
- UVa540 Team Queue
- C++中MessageBox()的详细用法(转载)
- asp.net 在使用Response.Redirect try{}catch{}块失效
- 让asp.net默认的上传组件支持进度条反映(转)
- iOS架构模式-MVP