java 过滤器调用链,如何在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?

解决方案

Adding following line of code in GreetingFilter works

filterChain.doFilter(servletRequest, servletResponse);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值