Java 在Spring HandlerInterceptor中,如何访问方法';s参数/参数?
我打算为SpringMVCContoller方法编写自己的@RoleRestricted注释,以实现类似SpringSecurity的功能。我想稍后集成到Spring Security,但现在,我必须有自己的解决方案。另外,这也是一个学习问题 我在Spring 3.2中使用了一个Java 在Spring HandlerInterceptor中,如何访问方法';s参数/参数?,java,spring,spring-mvc,spring-aop,spring-el,Java,Spring,Spring Mvc,Spring Aop,Spring El,我打算为SpringMVCContoller方法编写自己的@RoleRestricted注释,以实现类似SpringSecurity的功能。我想稍后集成到Spring Security,但现在,我必须有自己的解决方案。另外,这也是一个学习问题 我在Spring 3.2中使用了一个HandlerInterceptor,因此我的对象处理程序是HandlerMethod的一个实例 现在,我想要实现的是能够在注释的一些参数中使用SpEL表达式,这通常工作得很好,使用hm.getBean()作为SpEL计
HandlerInterceptor
,因此我的对象处理程序是HandlerMethod
的一个实例
现在,我想要实现的是能够在注释的一些参数中使用SpEL表达式,这通常工作得很好,使用hm.getBean()
作为SpEL计算上下文的根bean
最好是能够将处理程序方法参数也用作SpEL变量!例如,languageId可以指定为请求参数,并通过@RequestParam
成为处理程序参数
当从方法参数值生成键时,我试图查看Spring缓存的方法,但我发现这段代码传递了一个对象[]args
,该对象来自某个org.aopalliance包中的invoke()
方法。是否有办法仅使用Spring AOP获取对象[]args
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
if (handler instanceof HandlerMethod)
{
HandlerMethod hm = (HandlerMethod) handler;
RoleRestricted annotation = hm.getMethodAnnotation(RoleRestricted.class);
if (annotation != null)
{
// Handler method was annotated with an @RoleRestricted annotation.
HttpSession session = request.getSession();
RoleResource resource = annotation.resource();
EvaluationContext context = ???
if (SessionUtils.hasRoleLanguageAccess(annotation.resource(), session, annotation.corporateId(), annotation.languageId()))
{
return true;
}
else
{
logger.debug("Declined access");
response.sendRedirect(response.encodeRedirectURL("/RoleLogin.do?onSuccess=" + URLEncoder.encode(onSuccess, "UTF-8")));
}
}
}
return true;
}
HandlerInterceptor
不是SpringAOP,SpringAOP是带有对象[]
的MethodInvocation
。您应该使用这样的东西(或基于AspectJ的方面)来阻止方法的调用,引发异常,并且可能有一个HandlerExceptionResolver
处理该异常。不过,我强烈建议不要这样做,只需使用和学习Spring安全性(而不是在这两个步骤之间,这基本上是重新发明轮子)。谢谢!我同意春季安全。等我有时间就可以了。那么,包org.aopalliance是Spring的一部分?例外也是个好主意。我会看看我能做些什么,它现在可以很好地与HandlerInterceptor一起工作,除了参数的值,我可能会使用它。包org.aopalliance
不是spring的真正组成部分,而是在spring的开发过程中构思的。它曾经有一个更高的目标,但现在只有Spring使用它(而且只在旧的拦截器中使用)。一般来说,建议使用(更强大的)AspectJ方面。