I\'m using Spring-MVC with Spring Security for my web application. It includes user registration pages and private user panel. I have it set up currently with the following URL
All options are valid, it depends on the level of abstraction you want.
In a Filter
, you only have access to HttpServletRequest
and HttpServletResponse
objects, so you are very much coupled with the Servlet
API. You also don't (directly) have access to all the great Spring functionality like returning a view to be rendered or a ResponseEntity
.
In a HandlerInterceptor
, it's again more of the same. You can do your redirection or request handling directly in the preHandle()
where you don't have access to the ModelAndView
or set a flag which you check in postHandle()
. You would have access to the ModelAndView
but not to some other Spring MVC functionality.
Spring Security is a good alternative, but I find it has a lot of configuration that I don't like too much.
One final alternative, that I like the most, is to use AOP (you can do this with Spring Security or Shiro as well). You create an annotation like @Private
and you annotate your @Controller
handler methods. You use AOP to advise these methods. The advice basically checks some session or request attribute for a flag (authorized or not). If you are allowed, you continue executing the handler method, if not, you throw an UnauthorizedException
(or similar). You then also declare an @ExceptionHandler for that exception where you have pretty much complete control over how the response is generated: a ModelAndView
(and related), a ResponseEntity
, annotate the handler with @ResponseBody
, write the response directly, etc. I feel like you have much more control, if you want it.