spring-mvc - 如何在 Spring Interceptor preHandle 方法中获取 Controller 方法名称

标签 spring-mvc annotations interceptor

在我基于 spring mvc 和 spring security 的应用程序中,我使用 @Controller 注释来配置 Controller 。

我已经配置了 Spring Handler Interceptor 并且在 preHandle() 方法中,我想获取将被拦截器调用的方法名称。

我想在 HandlerInterceptorpreHandle() 方法中的 Controller 方法上定义自定义注解,以便我可以通过记录该特定方法的事件进行管理。

请看我的申请要求和代码

@Controller
public class ConsoleUserManagementController{
@RequestMapping(value = CONSOLE_NAMESPACE + "/account/changePassword.do", method = RequestMethod.GET)
@doLog(true)
public ModelAndView showChangePasswordPage() {
    String returnView = USERMANAGEMENT_NAMESPACE + "/account/ChangePassword";
    ModelAndView mavChangePassword = new ModelAndView(returnView);
    LogUtils.logInfo("Getting Change Password service prerequisit attributes");
    mavChangePassword.getModelMap().put("passwordModel", new PasswordModel());
    return mavChangePassword;
}
}

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
   // here I want the controller method name(i.e showChangePasswordPage() 
   // for /account/changePassword.do url ) to be called and that method annotation 
   // (i.e doLog() ) so that by viewing annotation , I can manage whether for that 
   // particular controller method, whether to enable logging or not.
}

我在我的应用程序中使用 SPRING 3.0

最佳答案

不知道 Handler 拦截器,但您可以尝试使用 Aspects 并为所有 Controller 方法创建通用拦截器。

使用方面,可以很容易地访问您的连接点方法名称。

您可以在方面内注入(inject)请求对象或使用:

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

从您的建议方法中检索它。

例如:

@Around("execution (* com.yourpackages.controllers.*.*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object doSomething(ProceedingJoinPoint pjp){
 pjp.getSignature().getDeclaringType().getName();
}

关于spring-mvc - 如何在 Spring Interceptor preHandle 方法中获取 Controller 方法名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8384903/

相关文章:

java - 并发请求的 repo 类线程安全吗? - Spring Boot

java - 限制 Rest Template 每分钟调用的电话数

java - 是否可以从winforms应用程序登录到使用spring security的网站?

angular - 在 Angular Interceptors 中获取当前路线

java - 如何处理拦截器绑定(bind)中的属性

java - 为什么我的自定义拦截器不起作用?

spring - Jasypt 加密在升级到 JDK 7u261 后停止工作

java - 如果用@Entity 和@Embeddable 注释类,是否应该抛出错误?

Java 元编程难题 : get all annotations that are themselves annotated by a given annotation A

ios - 在 map 中搜索注释