Spring MVC 3, Interceptor on all excluding some defined paths

后端 未结 3 1984
南方客
南方客 2020-12-24 05:28

Is it possible to apply an interceptor to all controllers and actions, except some that are defined?

Just to be clear, I am not interested in applying an interceptor

3条回答
  •  清歌不尽
    2020-12-24 05:39

    When configuring an interceptor, you can specify a path pattern. The interceptor will be invoked only for controllers which the path matches the interceptor path pattern.

    ref: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-config-interceptor

    But as you probably noticed it, the path pattern doesn't support exclusion.

    So I think the only way is to code a blacklist of paths inside the interceptor. When the interceptor is invoked, retrieve the HttpServletRequest.getRequestURI() and check if the path is blacklisted or not.

    You can build the blacklist inside a @PostConstruct annotated method of the interceptor, and so get the blacklisted path from a property file for instance.

提交回复
热议问题