如何在Spring Interceptor preHandle方法中获取控制器方法名称

发布于 2024-12-19 11:32:51 字数 1396 浏览 1 评论 0原文

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

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

我想要在 HandlerInterceptorpreHandle() 方法中的控制器方法上定义自定义注释,以便我可以通过记录该特定方法的活动进行管理。

请查看我的应用程序要求和代码

@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

In my application based on spring mvc and spring security I am using @Controller annotation to configure controller.

I have configured Spring Handler Interceptor and in preHandle() method , I want to get method name which is going to be call by interceptor.

I want to get custom annotation defined on controller method in preHandle() method of HandlerInterceptor so that I can manage by logging activity for that particular method.

Please have a look at my application requirement and code

@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.
}

I am using SPRING 3.0 in my application

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

极致的悲 2024-12-26 11:32:51

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

使用方面,可以轻松访问连接点方法名称。

您可以将请求对象注入方面或使用:

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();
}

Don't know about the Handler interceptor, but you could try to use Aspects and create a general interceptor for all your controller methods.

Using aspects, it would be easy to access your joinpoint method name.

You can inject the request object inside your aspect or use:

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

To retrieve it from your advice method.

For instance:

@Around("execution (* com.yourpackages.controllers.*.*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object doSomething(ProceedingJoinPoint pjp){
 pjp.getSignature().getDeclaringType().getName();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文