gpt4 book ai didi

java - 如何在 Spring Boot 中设置过滤器链?

转载 作者:IT老高 更新时间:2023-10-28 13:46:09 24 4
gpt4 key购买 nike

我遇到过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);
}

}

这里是 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));
}
}

这是过滤器配置:

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 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 时,只收到“Greetings from filter”并且不调用 HelloFilter

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

那么如何在 Spring boot 中设置过滤器链。上面的代码有bug吗?

最佳答案

在 GreetingFilter 中添加以下代码行

filterChain.doFilter(servletRequest, servletResponse);

关于java - 如何在 Spring Boot 中设置过滤器链?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38243381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com