Spring Boot with Filter
in Java with 0 comment

Spring Boot with Filter

in Java with 0 comment

@WebFilter

Use FilterRegistrationBean

Filter with Autowire

Code Piece

WebFilterConfiguration

@SpringBootConfiguration
public class WebFilterConfiguration {
  @Bean
  public FilterRegistrationBean<BasicAuthFilter> basicAuthFilterFilterRegistrationBean() {
    FilterRegistrationBean<BasicAuthFilter> filterFilterRegistrationBean =
      new FilterRegistrationBean<>();
    filterFilterRegistrationBean.setFilter(getBasicAuthFilter());
    filterFilterRegistrationBean.addUrlPatterns("/open/*");
    filterFilterRegistrationBean.setOrder(Ordered.LOWEST_PRECEDENCE - 1);
    return filterFilterRegistrationBean;
  }
  @Bean
  public BasicAuthFilter getBasicAuthFilter() {
    return new BasicAuthFilter();
  }
}

Filter

public class BasicAuthFilter implements Filter {
  @Autowired private PathMatcher pathMatcher;
  @Autowired private ResourceService resourceService;
  @Autowired private UrlPathHelper urlPathHelper;

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {}

  @Override
  public void doFilter(
  ......
}  

Cont.

Responses