Find the Registered Spring Security Filters

1. Overview

Spring Security is based on a chain of servlet filters. Each filter has a specific responsibility and depending on the configuration, filters are added or removed.

In this tutorial, we’ll discuss different ways to find the registered Spring Security Filters.

2. Security Debugging

First, we’ll enable security debugging which will log detailed security information on each request.

We can enable security debugging using the debug property:

@EnableWebSecurity(debug = true)

This way, when we send a request to the server, all the request information will be logged.

We’ll also be able to see the entire security filter chain:

Security filter chain: [
  WebAsyncManagerIntegrationFilter
  SecurityContextPersistenceFilter
  HeaderWriterFilter
  LogoutFilter
  UsernamePasswordAuthenticationFilter
  // ...
]

3. Logging

Next, we’ll find our security filters by enabling the logging for the FilterChainProxy.

We can enable logging by adding the following line to application.properties:

logging.level.org.springframework.security.web.FilterChainProxy=DEBUG

Here’s the related log:

DEBUG o.s.security.web.FilterChainProxy - /foos/1 at position 1 of 12 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
DEBUG o.s.security.web.FilterChainProxy - /foos/1 at position 2 of 12 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
DEBUG o.s.security.web.FilterChainProxy - /foos/1 at position 3 of 12 in additional filter chain; firing Filter: 'HeaderWriterFilter'
DEBUG o.s.security.web.FilterChainProxy - /foos/1 at position 4 of 12 in additional filter chain; firing Filter: 'LogoutFilter'
DEBUG o.s.security.web.FilterChainProxy - /foos/1 at position 5 of 12 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
...

4. Obtaining the Filters Programmatically

Now, we’ll see how to obtain the registered security filters programmatically.

We’ll use FilterChainProxy to get the security filters.

First, let’s autowire the springSecurityFilterChain bean:

@Autowired
@Qualifier("springSecurityFilterChain")
private Filter springSecurityFilterChain;

Here, we used a @Qualifier with the name springSecurityFilterChain with type Filter instead of FilterChainProxy. This is because the method of springSecurityFilterChain() in WebSecurityConfiguration, which creates the Spring Security filter chain, return type Filter and not FilterChainProxy.

Next, we’ll cast this object to FilterChainProxy and call the getFilterChains() method:

public void getFilters() {
    FilterChainProxy filterChainProxy = (FilterChainProxy) springSecurityFilterChain;
    List<SecurityFilterChain> list = filterChainProxy.getFilterChains();
    list.stream()
      .flatMap(chain -> chain.getFilters().stream()) 
      .forEach(filter -> System.out.println(filter.getClass()));
}

And here’s a sample output:

class org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter
class org.springframework.security.web.context.SecurityContextPersistenceFilter
class org.springframework.security.web.header.HeaderWriterFilter
class org.springframework.security.web.authentication.logout.LogoutFilter
class org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
...

Note that since Spring Security 3.1, FilterChainProxy is configured using a list of SecurityFilterChain. However, most applications need only one SecurityFilterChain.

5. Important Spring Security Filters

Finally, let’s take a look at some of the important security filters:

  • UsernamePasswordAuthenticationFilter: process authentication, responds by default to “/login” URL
  • AnonymousAuthenticationFilter: when there’s no authentication object in SecurityContextHolder, it creates an anonymous authentication object and put it there
  • FilterSecurityInterceptor: raise exceptions when access is denied
  • ExceptionTranslationFilter: catch Spring Security exceptions

6. Conclusion

In this quick articles, we explored how to find the registered Spring Security filters programmatically and using logs.

As always, source code can be found over on GitHub.

Related posts:

Converting between an Array and a List in Java
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Servlet 3 Async Support with Spring MVC and Spring Security
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
New Features in Java 12
Tính kế thừa (Inheritance) trong java
Period and Duration in Java
The XOR Operator in Java
Java Program to Implement Weight Balanced Tree
Lớp Properties trong java
Java Program to Implement Strassen Algorithm
HTTP Authentification and CGI/Servlet
Java Program to Implement Fenwick Tree
Implementing a Binary Tree in Java
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
HashMap trong Java hoạt động như thế nào?
Guide to the Java Queue Interface
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Jackson – Decide What Fields Get Serialized/Deserialized
Java Program to Implement Selection Sort
Phương thức forEach() trong java 8
Java Program to Implement SynchronosQueue API
Guide to @JsonFormat in Jackson
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Convert char to String in Java
Java Program to Perform Partial Key Search in a K-D Tree
REST Pagination in Spring
Java Program to Compute Cross Product of Two Vectors
OAuth 2.0 Resource Server With Spring Security 5