Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring WebMVC: interceptor which has access to the method definition and the HttpServletRequest

I'm trying to intercept Spring Controller calls which are annotated, similar to:

@RequestMapping("/my/page")
@AccessRestriction(module = Module.Audit, action = AuditActions.Log)
public ModelAndView myPage() {
   // pls type teh codez
} 

At this point I want to access both the values of the @AccessRestriction method, the HttpServletRequest object to check if the values match the restrictions and the HttpServletResponse object in order to send a redirect , if applicable. Being able to throw an exception might be suitable as well.

I've looked into Interceptors but they don't offer access to the method, just the handler. What are my options of achieving this?

like image 916
Robert Munteanu Avatar asked Aug 22 '26 02:08

Robert Munteanu


1 Answers

My suggestion would be to decouple the two concerns, one to check the annotation and throw an exception, another to catch that exception and translate it into a redirect.

The first concern could be done using the Auto-proxy facility, which would apply an AOP-style interceptor to any invocations on your controller objects. They would check for the annotation, validate the invocation, and throw a custom RuntimeException is the conditions are violated.

You could then have a custom HandlerInterceptor which checked for this exception in the afterCompletion method, sending a redirect via the response object if it's present.

like image 139
skaffman Avatar answered Aug 24 '26 05:08

skaffman