java - 如何在 Spring Boot 应用程序中注册 JavaEE 过滤器?

标签 java spring jakarta-ee spring-boot servlet-filters

我的基于 Spring BootREST 应用程序中有以下 Spring Boot Controller 和 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);
    }

}

SecurityFilter.java

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

}

BasicRestApplication.java

@SpringBootApplication
public class BasicRestApplication {

    public static void main(String[] args) {
        SpringApplication.run(BasicRestApplication.class, args);
    }

}

我已经浏览过这个链接How to add a filter class in Spring Boot?但它并没有明确说明需要添加什么内容以及在 Spring Boot 中注册过滤器的位置。

最佳答案

从这里找到解决方案Add a Servlet Filter in a Spring Boot application

创建了一个新的配置类并注册了过滤器类。

@Configuration
public class ApplicationConfig {

    @Bean
    public Filter securityFilter() {
        return new SecurityFilter();
    }

}

关于java - 如何在 Spring Boot 应用程序中注册 JavaEE 过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42399772/

相关文章:

java - 适用于 Android 的 Webrtc Aecm

spring - 问题将 spring-data-neo4j 独立 spring 项目连接在一起

java - Spring 和 Hibernate 2nd level with Ehcache

java - Jms java se 客户端和 javax.naming.NoInitialContextException

java - Jboss一步步设置热部署

Java 运行时相当于使用 javax Processor/google @AutoService 进行注释处理

java - Flink Quickstart 中 Eclipse IDE 中缺少依赖项

java - 为什么我的 Tomcat 部署的 web.xml 没有被主机服务器读取?

Java 依赖触发器

java - 实体Bean : Unable to retrieve EntityManagerFactory for unitName null