如何在Spring Boot中设置过滤器链? [英] How to set up filter chain in spring boot?

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

问题描述

我遇到过spring-boot,打算为传入的请求添加过滤器链.

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

这是应用程序:

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);
    }

}

这里是控制器:

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));
    }
}

这是过滤器配置:

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;
    }

}

这是HelloFilter和Greeting过滤器:

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() {

    }
}

当我启动应用程序并运行curl localhost:8080/greeting时,仅会收到来自过滤器的问候",而不会调用HelloFilter.

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

此外,Greeting Controller没有任何响应.似乎GreetingFilter阻止了该过程.

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

因此,如何在Spring Boot中设置过滤器链.上面的代码中有任何错误吗?

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

推荐答案

在GreetingFilter中添加以下代码行

Adding following line of code in GreetingFilter works

filterChain.doFilter(servletRequest, servletResponse);

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

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