Spring WebMVC: interceptor which has access to the method definition and the HttpServletRequest
I'm trying to intercept Spring Controll开发者_如何学编程er 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?
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.
精彩评论