Created
August 28, 2025 20:12
-
-
Save adbourne/63b3e7fc4ea1cd6a95e96ecd6b14afbb to your computer and use it in GitHub Desktop.
Log Spring Security Filter Chain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** Do not use in prod */ | |
| package com.devtiro.secureweb.services; | |
| import jakarta.servlet.Filter; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.boot.context.event.ApplicationReadyEvent; | |
| import org.springframework.context.event.EventListener; | |
| import org.springframework.security.web.DefaultSecurityFilterChain; | |
| import org.springframework.security.web.FilterChainProxy; | |
| import org.springframework.security.web.SecurityFilterChain; | |
| import org.springframework.stereotype.Component; | |
| import java.util.List; | |
| @Component | |
| public class SecurityFilterChainLogger { | |
| private static final Logger logger = LoggerFactory.getLogger(SecurityFilterChainLogger.class); | |
| @Autowired | |
| private FilterChainProxy filterChainProxy; | |
| @EventListener | |
| public void logFilterChain(ApplicationReadyEvent event) { | |
| logger.info("=== SPRING SECURITY FILTER CHAINS ==="); | |
| List<SecurityFilterChain> filterChains = filterChainProxy.getFilterChains(); | |
| for (int i = 0; i < filterChains.size(); i++) { | |
| SecurityFilterChain chain = filterChains.get(i); | |
| logger.info("Filter Chain {} ({})", i + 1, chain.getClass().getSimpleName()); | |
| // Check if we can get the request matcher | |
| if (chain instanceof DefaultSecurityFilterChain) { | |
| DefaultSecurityFilterChain defaultChain = (DefaultSecurityFilterChain) chain; | |
| logger.info(" Request Matcher: {}", defaultChain.getRequestMatcher()); | |
| } | |
| List<Filter> filters = chain.getFilters(); | |
| logger.info(" Filters ({}):", filters.size()); | |
| for (int j = 0; j < filters.size(); j++) { | |
| Filter filter = filters.get(j); | |
| logger.info(" [{}] {}", j + 1, filter.getClass().getSimpleName()); | |
| } | |
| logger.info(""); | |
| } | |
| logger.info("Total filter chains: {}", filterChains.size()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment