How to add a filter using WebMvcConfigurerAdapter in Spring?

With WebApplicationInitializer I can easily add a filter to ServletContext as part of the onStartup() method.

How to add a filter using WebMvcConfigurerAdapter ? Do I need to use XML?

ADD 1

To make it easier for other users to understand Spring’s more web-friendly configuration, I draw the following illustration.

Now you need to first understand the rational web configuration behind Spring. And then select which configuration class is inherited and which method to override from below.

It is less painful to look at him than to remember so many things.

enter image description here

And a good article on Spring Website Initialization:

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

ADD 2

Based on Tunaki answer Tunaki I checked AbstractDispatcherServletInitializer . Filter registration takes place in the following code:

enter image description here

Even I override the green getServletFilters() method, I still cannot get the result of Dyanmic registerServletFilter() . So, how do I configure the filter on addMappingForUrlPatterns() ?

I seem have to override the entire registerDispatcherServlet() method.

+5
source share
3 answers

WebMvcConfigurer is the interface that is used to configure Java configuration for Spring MVC enabled through @EnableWebMvc . WebMvcConfigurerAdapter is just an adapter that provides empty default methods for this interface.

It does not configure a DispatcherServlet that uses filters. Therefore, you cannot 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 the filter, you will have to instead of onStartup :

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

You can access the Dyanmic result of registerServletFilter() , as described in the configuration of your application (in particular, WebApplicationInitializer ):

 @Override public void onStartup(ServletContext servletContext) throws ServletException { // Create the 'root' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register( AppConfig.class, SecurityConfiguration.class, HibernateConfiguration.class ); // Add cuatom filters to servletContext FilterRegistration.Dynamic filterRegistration = servletContext.addFilter("recaptchaResponseFilter", new RecaptchaResponseFilter()); filterRegistration.setInitParameter("encoding", "UTF-8"); filterRegistration.setInitParameter("forceEncoding", "true"); filterRegistration.addMappingForUrlPatterns(null, true, "/*"); // Create the dispatcher servlet Spring application context AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext(); dispatcherServlet.register(MVCConfig.class); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); 
0
source

You can create spring beans that will implement Filter and @Inject ServletContext inside them. Then, in the @PostConstruct method, you can register them using servletContext.addServlet ("myFilter", this);

 public class MyFilter implements Filter { @Inject private ServletContext servletContext; @PostConstruct public void init(){ ServletRegistration.Dynamic filter = servletContext.addServlet("myFilter",this); filter.addMapping("/myMapping"); } } 

The bean should not be declared in the root context, which is initialized by ContextLoaderListener (rootContext), because the servlet 3.0 api forbids the use of dynamic registration for listeners. Therefore, it must be declared in the context manager, which is provided by the DispatcherServlet (dispatcherContext)

 public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { // Create the 'root' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); // Manage the lifecycle of the root application context container.addListener(new ContextLoaderListener(rootContext)); // Create the dispatcher servlet Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(DispatcherConfig.class); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } } 

spring doc

0
source

Source: https://habr.com/ru/post/1234958/


All Articles