Spring的AOP,Struts2的拦截器(Interceptor),以及springMVC的(interceptor)
参考外链:http://www.ibm.com/developerworks/cn/java/j-lo-springaopfilter/
1.首先,spring的AOP作用范围很广,可以使用Aspectj的execution表达式自定以切面的位置。
比如下面的配置service方法执行日志:
<!-- 系统日志 -->
<bean id="logUtils" class="com.tabchanj.crm.utils.SystemLogUtils">
<property name="logService" ref="systemLogService"></property>
</bean> <aop:config>
<aop:pointcut expression="execution(* com.tabchanj.crm.service..*.*(..))" id="logPointcut"/>
<aop:aspect ref="logUtils">
<aop:after method="writeLog" pointcut-ref="logPointcut" />
</aop:aspect>
</aop:config>
2.而struts2的拦截器,继承于AbstractInterceptor,并覆盖其中的intercept方法,它只能作用于action,对所有的action进行拦截。
package com.tabchanj.pss.web.interceptor; import java.util.Arrays;
import java.util.List; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.tabchanj.pss.domain.Employee;
import com.tabchanj.pss.service.IEmployeeService;
import com.tabchanj.pss.web.action.BaseAction; public class LoginInterceptor extends AbstractInterceptor { private static final long serialVersionUID = 3673324815465858383L;
private List<String> actionList;
private IEmployeeService employeeService; public void setEmployeeService(IEmployeeService employeeService) {
this.employeeService = employeeService;
} public void setActionList(String actionList) { this.actionList = Arrays.asList(actionList.split(","));
} @Override
public String intercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
if (actionList.contains(action.getClass().getSimpleName())) {
return invocation.invoke();
}
Employee employee = (Employee) ActionContext.getContext().getSession().get(BaseAction.LOGIN_USER);
if (employee == null) {
return Action.LOGIN;
}
// 查询出该action的全类名
String className = action.getClass().getName();
// 查询出当前所要访问的方法
String methodName = invocation.getProxy().getMethod();
// 将当前要访问的全类名和要访问的方法拼在一起
String classMethodName = className + "." + methodName;
// 拼出该action下的所有方法
String allclassMethodName = className + ".ALL";
// 查询出所有的资源method名称
List<String> allResourceMethod = employeeService.getAllResourcesMethod();
if (allResourceMethod.contains(classMethodName) || allResourceMethod.contains(allclassMethodName)) {
// 查询出该当前用户所用的资源列表
List<String> loginUserMethods = employeeService.findAllResourcesByLogin(employee.getId());
System.out.println("用户的资源2" + loginUserMethods);
if (loginUserMethods.contains(classMethodName) || loginUserMethods.contains(allclassMethodName)) {
return invocation.invoke();
} else {
return "auth";
}
}
return invocation.invoke();
} }
3.而springMVC的拦截器,只拦截Controller,其实现于HandlerInterceptor接口,并根据需要覆盖其中的3个分别在controller前后执行的方法,
package com.tabchanj.crm.web.interceptor; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import com.tabchanj.crm.domain.Employee;
import com.tabchanj.crm.utils.UserContext; public class AuthInterceptor implements HandlerInterceptor { public AuthInterceptor() {
System.out.println("AuthInterceptor初始化。。。。");
} public static final String LOGIN_PATH = "/checkLogin"; /**
* 1.在调用控制器方法前,拦截
*
* 返回值为false,代表拦截 返回值为true,代表放行
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// System.out.println("AuthInterceptor....preHandle");
/**
* 1、登陆验证
*/
// 获取session用户
Employee user = UserContext.getUser();
// 检查用户是否存在
if (user == null && !LOGIN_PATH.equals(request.getRequestURI())) {
response.sendRedirect("/login.jsp");
return false;
} /**
* 2、URL权限验证
*/
// 1、获取用户请求的地址
// 把handler转变成HandlerMethod对象
if (handler instanceof HandlerMethod) {
HandlerMethod hm = (HandlerMethod) handler;
// 获取控制器名称
String controllerName = hm.getBean().getClass().getName();
// 获取方法名;
String methodName = hm.getMethod().getName();
// 拼装resource名称
String resourceName = controllerName + ":" + methodName;
// 4.验证用户上,是否有请求资源对应的权限
if (UserContext.checkUserResPermission(resourceName)) {
return true;// 放行
} else {
response.sendRedirect("/data/noPermission.json");// 拦截,返回提示界面
return false; }
} return true;// 放行 } /**
* 2.在调用控制器方法后,拦截(在生成视图之前)
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// System.out.println("AuthInterceptor..postHandle....."); } /**
* 3.在视图生成之后(后台所有所有逻辑都完成后)
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// System.out.println("AuthInterceptor...afterCompletion....."); } }
Spring的AOP,Struts2的拦截器(Interceptor),以及springMVC的(interceptor)的更多相关文章
- 谈谈 Struts2 的拦截器
套话 相信非常多人都用过 Struts2 了,当然,对 Struts2 的原理也都比較了解.之前在一个项目中就已经用到了,当初的理解也不过局限在应用的层面上,对于更深层次的原理.机制,了解的并非非常多 ...
- struts2.1笔记03:AOP编程和拦截器概念的简介
1.AOP编程 AOP编程,也叫面向切面编程(也叫面向方面):Aspect Oriented Programming(AOP),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容.利用A ...
- Struts2的拦截器是如何使用AOP工作的
拦截器(interceptor)是Struts2最强大的特性之一,也可以说是struts2的核心,拦截器可以让你在Action和result被执行之前或之后进行一些处理.同时,拦截器也可以让你将通用的 ...
- Struts2自定义拦截器Interceptor以及拦截器登录实例
1.在Struts2自定义拦截器有三种方式: -->实现Interceptor接口 public class QLInterceptorAction implements Interceptor ...
- JavaWeb_(Struts2框架)拦截器interceptor
此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...
- JavaWeb框架_Struts2_(三)---->Struts2的拦截器
2. Struts2的拦截器(使用拦截器实现权限控制) 2.1 拦截器的概述 拦截器是Struts2的核心组成部分,它可以动态的拦截Action调用的对象,类似与Servlet中的过滤器.Struts ...
- struts2总结六: Struts2的拦截器
一.Struts2的系统结构图
- Struts2自定义拦截器处理全局异常
今天在整理之前的项目的时候想着有的action层没有做异常处理,于是想着自定义拦截器处理一下未拦截的异常. 代码: package cn.xm.exam.action.safeHat; import ...
- 【struts2】拦截器基础
1)拦截器是什么? 拦截器(Interceptor)是Struts2最强大的特性之一,它是一种可以让你在Action执行之前和Result执行之后进行一些功能处理的机制.来回顾一下官方给出的Strut ...
随机推荐
- shiro环境搭建及基本操作
一.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- js 中实现 汉字按拼音排序
let arr = ["贵州省", "江苏省", "江西省", "浙江省", "四川省", &quo ...
- 【JVM学习笔记】双亲委托机制存在的意义
1.可以确保Java核心库的类型安全:所有的Java应用都至少会引用java.lang.Object类,也就是说在运行期,java.lang.Object这个类会被加载到Java虚拟机:如果用户自定义 ...
- CSS练习-导航栏斜线分隔-利用伪元素
开始切第一张图了,第一个遇到的问题是顶部导航栏这里,用斜线分割.想到的思路是用伪类:before或者:after实现 先写html结构. <!-- 导航栏begin --> <div ...
- vue项目中使用组件化开发
最近在使用vue-cli结合webpack打包工具开发一个后台管理系统,使用vue难免需要运用组件化思想,而这也正是vue的一大特点. 在之前做的vue项目中,稍微有一点组件化的思想,可能是对组件化不 ...
- flask 之(五) --- 对象|钩子|拆分
内置对象 request: 请求的所有信息 session 服务端会话技术的接口 config: 当前项目的配置信息,模板中可以直接使用 g:global 在单次请求过程中,实现全局数据共享 ...
- 【AMAD】django-model-utils -- Django model使用的mixin和utils
动机 简介 个人评分 动机 为django model系统提供一些可重用的mixin和utils. 简介 django-model-utils1为Django Model提供了下嘛几种分类的utils ...
- Tensorflow实战第十一课(RNN Regression 回归例子 )
本节我们会使用RNN来进行回归训练(Regression),会继续使用自己创建的sin曲线预测一条cos曲线. 首先我们需要先确定RNN的各种参数: import tensorflow as tf i ...
- Redis高级主题
Redis高级主题 持久化 Redis 支持持久化, 其持久化数据有两种方式. 两种可以同时使用. 如果同时使用, Reids 在重启时将使用 AOF 方式来还原数据. RDB 按照一定策略定时同 ...
- linux中su和sudo区别
su切换用户,切换成root用户,要输入root用户的密码 su - 用户名 sudo 涉及到 /etc/sudoers文件 ,内容如下: # User privilege specificatio ...