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)
//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){ filters.add(filter); } public void execute(String request){ for (Filter filter : filters) { filter.execute(request); } //Execute the request after filtering target.execute(request); } public void setTarget(Target target){ this.target = target; } }
Create filter manager
//Manage filters public class FilterManager { FilterChain filterChain; public FilterManager(Target target){ filterChain = new FilterChain(); filterChain.setTarget(target); } public void setFilter(Filter filter){ filterChain.addFilter(filter); } //Filter request public void filterRequest(String request){ filterChain.execute(request); } }
Create Client
//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){ filterManager.filterRequest(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 client = new Client(); //Load the interceptor filter loader client.setFilterManager(filterManager); //Execute request start filtering client.sendRequest("HOME"); } }
Authentication request: HOME
Request log: HOME
Execution request: HOME