如何在Spring中使用WebMvcConfigurerAdapter添加过滤器? [英] How to add a Filter with WebMvcConfigurerAdapter in Spring?

查看:474
本文介绍了如何在Spring中使用WebMvcConfigurerAdapter添加过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用WebApplicationInitializer,我可以在onStartup()方法内轻松地向ServletContext添加过滤器.

With WebApplicationInitializer, I can easily add a filter to the ServletContext within the onStartup() method.

如何使用WebMvcConfigurerAdapter添加过滤器?我必须使用XML吗?

How to add a filter with WebMvcConfigurerAdapter? Do I have to use XML?

为帮助其他人更轻松地理解Spring Web Configuration,我画了下图.

To help others understand the Spring Web Configuration more easily, I draw the following illustration.

现在,您只需要首先了解Spring Web配置背后的rational.然后从下面选择要继承的配置类以及要覆盖的方法.

Now you just need to first understand the rational behind Spring Web configuration. And then pick up which config class to inherit and which method to override from below.

查找起来要比记住那么多事情要容易得多.

还有一篇有关Spring Web初始化的好文章:

And a good article on Spring Web Initialization:

http://www.kubrynski.com/2014/01/understanding-spring-web-initialization.html

基于Tunaki的回复,我检查了AbstractDispatcherServletInitializer.过滤器注册发生在以下代码中:

Based on Tunaki's reply, I checked the AbstractDispatcherServletInitializer. The filter registration happens in the following code:

即使我重写了绿色的getServletFilters()方法,我仍然无法访问registerServletFilter()Dyanmic结果.那么如何通过addMappingForUrlPatterns()配置过滤器?

Even I override the green getServletFilters() method, I still cannot access the Dyanmic result of the registerServletFilter(). So how can I configure the filter by addMappingForUrlPatterns()?

似乎我have to覆盖了整个registerDispatcherServlet()方法.

It seems I have to override the whole registerDispatcherServlet() method.

推荐答案

WebMvcConfigurer是用于自定义通过@EnableWebMvc启用的Spring MVC的基于Java的配置的接口. WebMvcConfigurerAdapter只是一个适配器,为此接口提供默认的空方法.

WebMvcConfigurer is an interface that is used to customize the Java-based configuration for Spring MVC enabled via @EnableWebMvc. WebMvcConfigurerAdapter is just an adapter providing default empty methods for this interface.

它没有配置DispatcherServlet,这是使用过滤器的.因此,您不能使用WebMvcConfigurer来配置servlet过滤器.

It does not configure the DispatcherServlet, which is what filters are used by. As such, you can't use WebMvcConfigurer to configure servlet filters.

要轻松配置过滤器,您可以从

To easily configure filters, you can inherit from AbstractDispatcherServletInitializer and override getServletFilters():

public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {

    @Override
    protected Filter[] getServletFilters() {
        return new Filter[] { new CharacterEncodingFilter() };
    }

}

如果要进一步配置过滤器,则必须覆盖

If you want to further configure a filter, you will have to override onStartup instead:

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    super.onStartup(servletContext);
    servletContext.addFilter("name", CharacterEncodingFilter.class)
                  .addMappingForUrlPatterns(null, false, "/*");
}

这篇关于如何在Spring中使用WebMvcConfigurerAdapter添加过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆