Java 如何在spring boot中设置过滤链?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38243381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:07:57  来源:igfitidea点击:

How to set up filter chain in spring boot?

javaspringspring-bootservlet-filters

提问by chenatu

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

我遇到了 spring-boot 并打算为传入的请求添加一个过滤器链。

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:

这是 HelloFilter 和 Greeting 过滤器:

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 HelloFilteris not invoked.

当我启动应用程序并运行时curl localhost:8080/greeting,只收到“来自过滤器的问候”并且HelloFilter没有调用。

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

此外没有任何回应Greeting Controller。似乎GreetingFilter阻止了程序。

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

那么如何在Spring boot中设置过滤链。上面的代码是否有任何错误?

采纳答案by Gangadhar

Adding following line of code in GreetingFilter works

在 GreetingFilter 中添加以下代码行有效

filterChain.doFilter(servletRequest, servletResponse);

回答by Ankit Basarkar

I would just like to clarify a little more on what Gangadhar suggested. You can try adding :

我只想进一步澄清 Gangadhar 的建议。您可以尝试添加:

filterChain.doFilter(servletRequest, servletResponse);

filterChain.doFilter(servletRequest, servletResponse);

in doFilter method of your filter classes.This will create chaining of the filters.

在过滤器类的 doFilter 方法中。这将创建过滤器的链接。