How do I ensure a filter is executed after security filters in the SecurityFilterChain?

I have a BearerTokenAuthenticationFilter which is registered through a SecurityFilterChain. Additionally, there is a ProfileSynchronizationFilter which is registered via a FilterRegistrationBean.

The BearerTokenAuthenticationFilter needs to be executed before the ProfileSynchronizationFilter. Right now the opposite happens.

My understanding is that filters in the SecurityFilterChain is executed by a DelegatingFilterProxy. So, I assume it is the DelegatingFilterProxy which must be ordered relative to the ProfileSynchronizationFilter. However, I’m not sure if that is the right way – or even how to do that.

Excerpt from the Spring docs :

https://docs.spring.io/spring-security/reference/servlet/architecture.html#servlet-filterchainproxy

How do I solve this ordering issue so that the BearerTokenAuthenticationFilter gets executed before the ProfileSynchronizationFilter?

Configuration of the SecurityFilterChain :

@Bean
@Order(2)
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  http.csrf()
    .disable()
    .sessionManagement()
    .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    .and()
    .requestMatchers()
    .antMatchers(listOfSecuredResources)
    .and()
    .authorizeRequests()
    .antMatchers(listOfSecuredResources)
    .fullyAuthenticated()
    .and()
    .exceptionHandling()
    .authenticationEntryPoint(authenticationEntryPoint)
    .accessDeniedHandler(accessDeniedHandler)
    .and()
    .addFilter(jwtAuthenticationFilter)
    .anonymous()
    .disable();

  return http.build();
}

Configuration of the ProfileSynchronizationFilter :

@Bean
public FilterRegistrationBean registerProfileSynchronizationFilter(ProfileSynchronizationFilter profileSynchronizationFilter) {
  FilterRegistrationBean reg = new FilterRegistrationBean(profileSynchronizationFilter);
  reg.setOrder(6);
  return reg;
}

6

The illustration posted in the original post outlines filters and security filters very well. A DelegatingFilterProxy (springSecurityFilterChain) is installed as a filter by Spring. As the class name says, this filter delegates filtering to security filters.

Security filters can not be ordered relative to regular filters. Security filters can only be ordered relative to other security filters.

To get an understanding of both regular filters as well as security filters in your Spring Boot project, you can use the below class which will list all filters upon application startup.

Based on this list, I was able to troubleshoot the ordering of filters.

For the specific issue I described in the original post, one reason for the unpredictable execution order was that the security filter was exposed as a Bean. Spring thus installed the security filter as both a regular filter as well as a security filter:

Springboot – filters automatically registered

@Component
public class FilterPrinter implements CommandLineRunner {

        @Autowired
        List<SecurityFilterChain> securityFilterChains;

        @Override
        public void run(String... args) throws Exception {
                for (int i = 0; i < securityFilterChains.size(); i++) {
                        SecurityFilterChain securityFilterChain = securityFilterChains.get(i);
                        System.out.println("security chain #" + (i + 1) + " filters:");
                        System.out.println("-----");
                        List<Filter> filters = securityFilterChain.getFilters();
                        filters.forEach(filter
                            -> System.out.println("- " + filter.getClass().getSimpleName())
                        );
                }

        }

        @Bean
        public CommandLineRunner cmdLineRunner(ApplicationContext context) {
                return args -> {
                        ServletContextInitializerBeans scib = new ServletContextInitializerBeans(context,
                            FilterRegistrationBean.class, DelegatingFilterProxyRegistrationBean.class);
                        System.out.println("request filters:");
                        System.out.println("----");
                        scib.iterator().forEachRemaining(s -> {
                                System.out.println("- " + s);
                        });
                };
        }

}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật