Java interceptor filter pattern

Keywords: Java Design Pattern Algorithm

Java interceptor filter pattern

Intercepting Filter Pattern is used to pre / post process the request or response of the application. Define filters and apply them to requests before passing them to the actual target application. The filter can do authentication / authorization / logging, or track requests, and then pass the requests to the corresponding handler. The following are the entities of this design pattern.

  • Filter - the filter performs certain tasks before or after the request handler executes the request.
  • Filter Chain - the Filter Chain has multiple filters and executes them in the defined order on the Target.
  • Target - the target object is the request handler.
  • Filter Manager - the Filter Manager manages filters and filter chains.
  • Client - the client is the object that sends the request to the Target object.

We have learned the java filter pattern before, so what is the difference between it and the java intercept filter pattern?

The java filter pattern can implement filtering anywhere

The filters we use in Spring use this pattern

The java interception filter mode generally only works on request filtering

And we use this pattern in the spring MVC interceptor

We just set up the structure and how to intercept it, so we need to be more specific about the event situation

Create a Filter interface Filter.

//Filter interface
public interface Filter {
    public void execute(String request);

Create a solid filter part.

// Filter condition
public class AuthenticationFilter implements Filter {
    public void execute(String request){
        System.out.println("Authentication request: " + request);
// Filter condition
public class DebugFilter implements Filter {
    public void execute(String request){
        System.out.println("request log: " + request);

Create Target. (request)

public class Target {
    public void execute(String request){
        System.out.println("Execute request: " + request);

Create filter

//Assemble the filter parts into a filter
public class FilterChain {
    private List<Filter> filters = new ArrayList<Filter>();
    private Target target;

    public void addFilter(Filter filter){

    public void execute(String request){
        for (Filter filter : filters) {
        //Execute the request after filtering

    public void setTarget(Target target){ = target;

Create filter manager

//Manage filters
public class FilterManager {
    FilterChain filterChain;

    public FilterManager(Target target){
        filterChain = new FilterChain();
    public void setFilter(Filter filter){
    //Filter request
    public void filterRequest(String request){

Create Client

public class Client {
    FilterManager filterManager;
    //Load filter
    public void setFilterManager(FilterManager filterManager){
        this.filterManager = filterManager;
    //Pass request into filter
    public void sendRequest(String request){

Use the Client to demonstrate the interceptor filter design pattern.

public class InterceptingFilterDemo {
    public static void main(String[] args) {
        //Add request
        FilterManager filterManager = new FilterManager(new Target());
        //Add filter content
        filterManager.setFilter(new AuthenticationFilter());
        filterManager.setFilter(new DebugFilter());
        Client client = new Client();
        //Load the interceptor filter loader
        //Execute request start filtering

Authentication request: HOME
Request log: HOME
Execution request: HOME

Like - collect - pay attention - easy to review and receive the latest content in the future If you have other questions to discuss in the comment area - or send me a private letter - you will reply as soon as you receive them In case of infringement, please contact me by private letter Thank you for your cooperation. I hope my efforts will be helpful to you^_^

Posted by parawizard on Wed, 29 Sep 2021 21:55:14 -0700