简体   繁体   中英

Adding a filter to the Servlet configuration in Spring Boot

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

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM