在Spring Boot中为Servlet配置添加过滤器 [英] Adding a filter to the Servlet configuration in Spring Boot

查看:68
本文介绍了在Spring Boot中为Servlet配置添加过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring-boot-starter-web捆绑包构建基于spring-batch-admin的web应用.

I am using the spring-boot-starter-web bundle to build a spring-batch-admin based webapp.

@Configuration
@EnableAutoConfiguration(exclude = { BatchAutoConfiguration.class, DataSourceAutoConfiguration.class, WebMvcAutoConfiguration.class })
@Import(MainConfiguration.class)
@EnableTransactionManagement
public class BatchAdmin extends SpringBootServletInitializer {

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

@Configuration
@ComponentScan("com.company.package*")
@Import({ ServletConfiguration.class, WebappConfiguration.class })
public class MainConfiguration {}

@Configuration
@ImportResource("classpath:/org/springframework/batch/admin/web/resources/servlet-config.xml")
public class ServletConfiguration {}

@Configuration
@ImportResource({ "classpath:/org/springframework/batch/admin/web/resources/webapp-config.xml","classpath:persistence-context.xml" })
public class WebappConfiguration {}

如何使用Java配置样式将此过滤器添加到servlet上下文中?

How can I add this filter to the servlet context using the java configuration style?

<filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

推荐答案

有时候(但不经常),比起问问SO社区,查阅文档要快得多.

Sometimes (but not often) it's faster to consult the documentation than asking the SO community.

根据官方

According to the official spring-boot documentation, I had to add the following bean definition to the BatchAdmin class.

@Bean
public Filter hiddenHttpMethodFilter() {
   HiddenHttpMethodFilter filter = new HiddenHttpMethodFilter();
   return filter;
}

这篇关于在Spring Boot中为Servlet配置添加过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆