Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a Servlet Filter in a Spring Boot application

I'd like to have ETag suport. For this purpose there is a ShallowEtagHeaderFilter which does all the work. How can I add it without declaring it in my web.xml (which actually does not exist, because I somehow got by without it so far)?

P.S. I use Spring Boot 1.1.4

P.P.S. Here's a full solution

package cuenation.api;

import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.ShallowEtagHeaderFilter;

import javax.servlet.DispatcherType;
import java.util.EnumSet;

@Configuration
public class WebConfig {

    @Bean
    public FilterRegistrationBean shallowEtagHeaderFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new ShallowEtagHeaderFilter());
        registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
        registration.addUrlPatterns("/cue-categories");
        return registration;
    }

}
like image 774
dVaffection Avatar asked Oct 01 '14 21:10

dVaffection


People also ask

What is servlet filter in spring boot?

A filter is an object used to intercept the HTTP requests and responses of your application. By using filter, we can perform two operations at two instances − Before sending the request to the controller. Before sending a response to the client.

What is the purpose of filter in spring boot?

In Spring boot, we have filters to filter the HTTP request; filter, in general, is used to intercept the request, i.e. HTTP request and the response from the client-side. By the use of a filter, we can perform two operations which can be done on response and request.

What is the use of FilterRegistrationBean?

The FilterRegistrationBean is, as the name implies, a bean used to provide configuration to register Filter instances. It can be used to provide things like URL mappings etc.


2 Answers

When using Spring Boot

As mentioned in the reference documentation, the only step needed is to declare that filter as a Bean in a configuration class, that's it!

@Configuration public class WebConfig {    @Bean   public Filter shallowEtagHeaderFilter() {     return new ShallowEtagHeaderFilter();   } } 

When using Spring MVC

You're probably already extending a WebApplicationInitializer. If not, then you should convert your webapp configuration from a web.xml file to a WebApplicationInitializer class.

If your context configuration lives in XML file(s), you can create a class that extends AbstractDispatcherServletInitializer - if using configuration classes, AbstractAnnotationConfigDispatcherServletInitializer is the proper choice.

In any case, you can then add Filter registration:

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

Full examples of code-based Servlet container initialization are available in the Spring reference documentation.

like image 100
Brian Clozel Avatar answered Sep 24 '22 00:09

Brian Clozel


A bit late answer.

My solution was to create custom annotation:

import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;

// ...

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface Filter {

    @AliasFor(annotation = Component.class, attribute = "value")
    String value() default "";

}

And then simply apply it to the filter implementations:

@Filter
public class CustomFilter extends AbstractRequestLoggingFilter {

    @Override
    protected void beforeRequest(HttpServletRequest request, String message) {
        logger.debug("before req params:", request.getParameterMap());
    }

    @Override
    protected void afterRequest(HttpServletRequest request, String message) {
        logger.debug("after req params:", request.getParameterMap());
    }
}

See more: @AliasFor, Spring custom annotations question

like image 29
Andrii Abramov Avatar answered Sep 27 '22 00:09

Andrii Abramov