Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up filter chain in spring boot?

I have come across spring-boot and intend to add a filter chain for incoming request.

Here is the Application:

package example.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }

}

Here is the Controller:

package example.hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicLong;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                String.format(template, name));
    }
}

Here is the Filter Config:

package example.hello;

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

@Configuration
public class WebConfig {

    @Bean
    public FilterRegistrationBean greetingFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setName("greeting");
        GreetingFilter greetingFilter = new GreetingFilter();
        registrationBean.setFilter(greetingFilter);
        registrationBean.setOrder(1);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean helloFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setName("hello");
        HelloFilter helloFilter = new HelloFilter();
        registrationBean.setFilter(helloFilter);
        registrationBean.setOrder(2);
        return registrationBean;
    }

}

Here is the HelloFilter and Greeting Filter:

package example.hello;

import javax.servlet.*;
import java.io.IOException;

public class HelloFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("HelloFilter!");
    }

    @Override
    public void destroy() {

    }
}

package example.hello;

import javax.servlet.*;
import java.io.IOException;

public class GreetingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("Greetings from filter!");
    }

    @Override
    public void destroy() {

    }
}

When I start the application and run curl localhost:8080/greeting, Only "Greetings from filter" is received and the HelloFilter is not invoked.

Besides there is no response from Greeting Controller. It seems that the GreetingFilter blocks the procedure.

So how to set the filter chain in Spring boot. Are there any bugs in the code above?

like image 205
chenatu Avatar asked Jul 07 '16 10:07

chenatu


People also ask

How do I set Spring boot filters?

There are three ways to add your filter, Annotate your filter with one of the Spring stereotypes such as @Component. Register a @Bean with Filter type in Spring @Configuration. Register a @Bean with FilterRegistrationBean type in Spring @Configuration.

How do Spring boot filter chains work?

Spring Security maintains a filter chain internally where each of the filters has a particular responsibility and filters are added or removed from the configuration depending on which services are required. The ordering of the filters is important as there are dependencies between them.

How do I register multiple filters in Spring boot?

Add the annotation @ServletComponentScan to the spring boot main class, which will scan all servlet-related classes in the spring boot application context. You can use the url pattern to limit request control based on the request url. If you add multiple filters in a sequence, they will all run one at a time.

How do I configure DelegatingFilterProxy?

public class MyWebAppInitializer implements WebApplicationInitializer { ... @Override public void onStartup(ServletContext servletContext) throws ServletException { ... servletContext. addFilter("myFilter", new DelegatingFilterProxy("myFilter")) .


1 Answers

Adding following line of code in GreetingFilter works

filterChain.doFilter(servletRequest, servletResponse);
like image 109
Gangadhar Avatar answered Sep 28 '22 02:09

Gangadhar