Spring中没有HandlerInterceptor如何拦截所有控制器的所有请求?

发布于 2024-12-24 03:55:01 字数 160 浏览 2 评论 0原文

我有一个需求,在spring 2.5中拦截所有请求。我不想使用 HandlerInterceptor 来拦截请求,因为它需要使用上下文文件中的每个 SimpleUrlHandlerMapping bean 进行配置。是否有另一种方法可以在不使用 HandlerInterceptor 的情况下拦截所有请求?

I have a requirement to intercept all the request in spring 2.5. I don't want to use HandlerInterceptor to intercept the request because it requires to configure it with every SimpleUrlHandlerMapping bean in the context files. Is there another way to intercept all the request without using HandlerInterceptor?

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

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

发布评论

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

评论(1

在风中等你 2024-12-31 03:55:01

您可以实现一个过滤器并将其映射到 web.xml 中的 DispatcherServlet。那么您应该能够拦截对 Spring MVC 发出的所有请求。

简而言之:

  1. 创建 javax.servlet.Filter 的实现
  2. 将过滤器添加到 web.xml

    <前><代码><过滤器>
    <过滤器名称>MyFilter
    <过滤器类>mypackage.MyFilter

    然后将其映射到 DispatcherServlet(servlet-name 应该与为 Spring Dispatcher servlet 定义的相同。

    <过滤器映射>
        <过滤器名称>MyFilter
        DispatcherServlet
    
    

如果需要访问过滤器中的 Spring ApplicationContext ,使用静态方法

   org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext()

You could implement a filter and map it to the DispatcherServlet in web.xml. Then you should be able to intercept all request made to Spring MVC.

In short:

  1. Create an implementation of javax.servlet.Filter
  2. Add the filter to web.xml

    <filter>
     <filter-name>MyFilter</filter-name>
     <filter-class>mypackage.MyFilter</filter-class>
    </filter>
    

    and then map it to the DispatcherServlet (servlet-name should be the same that is defined for the Spring Dispatcher servlet.

    <filter-mapping>
        <filter-name>MyFilter</filter-name>
        <servlet-name>DispatcherServlet</servlet-name>
    </filter-mapping>
    

If you need access to the Spring ApplicationContext in the filter, use the static method

   org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文