我遇到了spring-boot,並打算爲傳入的請求添加一個過濾器鏈。如何在彈簧啓動時設置濾波器鏈?
這裏是應用:
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和問候過濾:
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
。
此外,Greeting Controller
沒有響應。看來GreetingFilter
會阻止該過程。
那麼如何在Spring引導中設置過濾器鏈。上面的代碼中是否有錯誤?
您的代碼有缺陷...您在第一個過濾器停止/阻止te請求時永遠不會傳遞。你需要調用'filterChain.doFilter(request,response)'來繼續調用... –