如何在Spring Boot应用程序中注册JavaEE过滤器?
在基于如何在Spring Boot应用程序中注册JavaEE过滤器?,java,spring,jakarta-ee,spring-boot,servlet-filters,Java,Spring,Jakarta Ee,Spring Boot,Servlet Filters,在基于Spring-Boot的REST应用程序中,我有以下Spring-Boot控制器和JavaEE过滤器类。请注意,我没有在这里配置web.xml http://localhost:8080/api/products -> Returns 200 问题是截取器/过滤器从未被调用 ProductController.java @RestController @RequestMapping("/api") public class ProductController { @Inj
Spring-Boot
的REST
应用程序中,我有以下Spring-Boot
控制器和JavaEE
过滤器类。请注意,我没有在这里配置web.xml
http://localhost:8080/api/products -> Returns 200
问题是截取器/过滤器从未被调用
ProductController.java
@RestController
@RequestMapping("/api")
public class ProductController {
@Inject
private ProductService productService;
//URI: http://localhost:8080/api/products
@RequestMapping(value = "/products", method = RequestMethod.GET)
public ResponseEntity<Iterable<Product>> getAllProducts() {
Iterable<Product> products = productService.getAllProducts();
return new ResponseEntity<>(products, HttpStatus.OK);
}
//URI: http://localhost:8080/api/products/50
@RequestMapping(value = "/products/{productId}", method = RequestMethod.GET)
public ResponseEntity<?> getProduct(@PathVariable Long productId) {
Product product = productService.getProduct(productId);
return new ResponseEntity<>(product, HttpStatus.OK);
}
}
@WebFilter(urlPatterns = {"/api/products/*"})
public class SecurityFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//in the init method
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//THIS METHOD NEVER GETS CALLED AND HENCE THIS NEVER GETS PRINTED ON CONSOLE.
//WHY ?????
System.out.println("**********Into the doFilter() method...........");
final HttpServletRequest httpRequest = (HttpServletRequest) request;
final HttpServletResponse httpResponse = (HttpServletResponse) response;
//move ahead
chain.doFilter(httpRequest, httpResponse);
}
@Override
public void destroy() {
//nothing to implement
}
}
@SpringBootApplication
public class BasicRestApplication {
public static void main(String[] args) {
SpringApplication.run(BasicRestApplication.class, args);
}
}
BasicRestApplication.java
@RestController
@RequestMapping("/api")
public class ProductController {
@Inject
private ProductService productService;
//URI: http://localhost:8080/api/products
@RequestMapping(value = "/products", method = RequestMethod.GET)
public ResponseEntity<Iterable<Product>> getAllProducts() {
Iterable<Product> products = productService.getAllProducts();
return new ResponseEntity<>(products, HttpStatus.OK);
}
//URI: http://localhost:8080/api/products/50
@RequestMapping(value = "/products/{productId}", method = RequestMethod.GET)
public ResponseEntity<?> getProduct(@PathVariable Long productId) {
Product product = productService.getProduct(productId);
return new ResponseEntity<>(product, HttpStatus.OK);
}
}
@WebFilter(urlPatterns = {"/api/products/*"})
public class SecurityFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//in the init method
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//THIS METHOD NEVER GETS CALLED AND HENCE THIS NEVER GETS PRINTED ON CONSOLE.
//WHY ?????
System.out.println("**********Into the doFilter() method...........");
final HttpServletRequest httpRequest = (HttpServletRequest) request;
final HttpServletResponse httpResponse = (HttpServletResponse) response;
//move ahead
chain.doFilter(httpRequest, httpResponse);
}
@Override
public void destroy() {
//nothing to implement
}
}
@SpringBootApplication
public class BasicRestApplication {
public static void main(String[] args) {
SpringApplication.run(BasicRestApplication.class, args);
}
}
我已经浏览了这个链接,但是它没有给出一个关于需要添加什么的清晰想法,在哪里注册Spring Boot的过滤器从这里找到了解决方案 创建了新的配置类并注册了筛选器类
@Configuration
public class ApplicationConfig {
@Bean
public Filter securityFilter() {
return new SecurityFilter();
}
}