论坛首页 Java企业应用论坛

用AOP拦截spring mvc 控制层

浏览 8241 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (19) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-07-09  

最近打算把权限控制这一块使用AOP方式来做,

本人使用的MVC框架是是SPRING 自带的MVC框架,

按方法写了一个MethodInterceptor

配置好ProxyFactorybean对action实例进行代理

运行没问题,但发现不能对action中某个具体方法拦截。

查看了一下 spring的 MultiActionController类的源代码,

发现它只有一个调用入口方法:
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
	try {
		String methodName = this.methodNameResolver.getHandlerMethodName(request);
		return [color=red]invokeNamedMethod[/color](methodName, request, response);
	}
	catch (NoSuchRequestHandlingMethodException ex) {
		return handleNoSuchRequestHandlingMethod(ex, request, response);
	}
}

protected final ModelAndView invokeNamedMethod(String methodName, HttpServletRequest request, HttpServletResponse response) throws Exception {

	Method method = (Method) this.handlerMethodMap.get(methodName);
	if (method == null) {
		throw new NoSuchRequestHandlingMethodException(methodName, getClass());
	}

	try {
		List params = new ArrayList(4);
		params.add(request);
		params.add(response);
				
		if (method.getParameterTypes().length >= 3 && method.getParameterTypes()[2].equals(HttpSession.class) ){
			HttpSession session = request.getSession(false);
			if (session == null) {
				throw new HttpSessionRequiredException(
						"Pre-existing session required for handler method '" + methodName + "'");
			}
			params.add(session);
		}
			
		// If last parameter isn't of HttpSession type, it's a command.
		if (method.getParameterTypes().length >= 3 &&
				!method.getParameterTypes()[method.getParameterTypes().length - 1].equals(HttpSession.class)) {
			Object command = newCommandObject(method.getParameterTypes()[method.getParameterTypes().length - 1]);
			params.add(command);
			bind(request, command);
		}
			
		Object returnValue = method.invoke(this.delegate, params.toArray(new Object[params.size()]));
		return massageReturnValueIfNecessary(returnValue);
	}
	catch (InvocationTargetException ex) {
		// The handler method threw an exception.
		return handleException(request, response, ex.getTargetException());
	}
	catch (Exception ex) {
		// The binding process threw an exception.
		return handleException(request, response, ex);
	}
}




invokeNamedMethod 最终使用了反射机制调用该方法, 是不是这样就不能被AOP拦截了??

不知道各位大侠怎么解决的
   发表时间:2008-07-10  
为什么不用spring security呢?
0 请登录后投票
   发表时间:2008-07-10  
不能拦截,你的切点表达式写的对不对?
0 请登录后投票
   发表时间:2008-07-10  
bottom 写道
为什么不用spring security呢?


谢谢提醒,有空去研究一下

bottom 写道
不能拦截,你的切点表达式写的对不对?


我测试是拦截到所有方法,toString()方法被拦截了,还有一些public方法被拦截了,我想问题出在这里:

对于类似DispatcheAction类,都有一个方法调用入口 如:execute();

而在execute()方法内部使用了反射调用自己类的一些其它方法,
public Object execute() {
    Method method = getMethod(param);
    //这里可能是method1, method2....
    method.invoke(....);
    
}

public Object method1() {
    //...
};

public Object method2() {
    //....
}


拦截execute()方法没问题,

我现在想对上面的method1 和method2 方法进行aop拦截, 该怎么做???
0 请登录后投票
   发表时间:2008-07-13  
考虑外层拦截吧,基于proxy的aop实现的老问题了,呵呵
0 请登录后投票
   发表时间:2008-08-05  
考虑使用acegi,根据业务,定义投票器,拦截url做权限控制,可以解决绝大部分的权限问题。我们现在就是这样做的。
我们目前的权限控制就是这样做的,定义3个投票器:
1.是否登陆。
2.是否可以访问当前url。
3.当前访问url的数据是否在自己允许的范围内。
0 请登录后投票
   发表时间:2008-08-07  
Acegi好东西,一般没有什么特殊需求的权限控制,仅仅需要配置配置,写一两个AuthenticationProvider类就可以了,非常爽,而且对其他开发人员来说是完全透明的,也就是说他们根本无需关心权限控制,自己的模块该怎么写还是怎么写
0 请登录后投票
   发表时间:2008-08-07  
自己正在写一个轻量的AOP工具,目的就是在没有Spring或者其他AOP框架的情况下也能得到AOP服务。原理当然是基于Java的动态代理,但是大家都知道这个要运作起来的前提是需要有Interface,也就是需要被增强(无论是前置后置还是环绕性的)的方法必须是在一个接口中申明了的。
这样问题就来了,如果我要对我的领域模型对象的方法进行增强,我就需要为这些对象重新写接口(在领域建模的时候我没有写接口的),这样的话好像违反了POJO原理,而且感觉为每一个业务对象写接口好像本末倒置了(正常情况下应该是规定好了接口,然后才实现之)。是不是应该切在Action层而不是Service层才对?
不知道Spring中是怎么实现的,请了解的朋友说明一下


另外一个问题,不知道有没有人采用aopalliance来写aop
其中MethodInvocation中的proceed()方法是将处理向下发送到下一个MethodInterceptor,但是API中并没有给出如果将MethodInterceptor的集合跟MethodInvocation关联(或者叫注册)的方法,一个 MethodInvocation如果知道这个切入点上有哪些增强处理或者下一个增强是谁呢?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics