Implementation and difference between springboot filters and interceptors

foreword

There are two common AOP implementations in springmvc:
1. Filter
2. Interceptor

This article is for some people who are new to springboot,
so it mainly explains the simple implementation of filter and interceptor and what is the difference between them
(I will post some complex functions later, please remember to pay attention)

Simple implementation of Filter

Literal meaning: filter is the function of filtering. In web development, filter some URLs we specify,
so what can it help us filter?
There are more functions:
such as intercepting interface requests that we do not need,
modifying the content of requests and responses,
completing CORS cross-domain requests, etc.

Now let's implement a simple filter:
you can create a new filter package, and as the project expands, there will be more and more filters.
Here I create a new TestFilterclass to implement the Filter interface

 

Let's go step by step
1. @Component injects this class into the IOC container
2. @WebFilter(urlPatterns = "/Blogs", filterName = "blosTest") indicates that this is a web filter, the url it intercepts is /Blogs, filter device name isblogsTest

The three refactoring methods after implementing the interface are posted below:

 

The initialization (init) and destroy (destroy) methods are generally not used. For specific use, look at the source code to know
that doFilter() is the core of the filter.
Note: After implementing the interface method, we need to convert the request and response types to HttpServlet, otherwise The next operation may report an error.
If the filter is passed, the execution filterChain.doFilter(request,response);
indicates that this url has passed our Filter
and others do not need to be set. Let's execute it and see the result:

As you can see, we only need one class to implement a simple filter

Of course, you can configure the startup class without annotations

 

This is also possible. In fact, I still recommend this method to add filters.

Simple implementation of Interceptor

The implementation of the interceptor is a little more complicated than the filter.
We can also create a new interceptor package and create a new class
named inside it .MyInterceptor

 

This class implements the HandleInterceptorinterface
. Paste the class code directly, and I will annotate the function in the code.

 

It implements three methods in turn.
Compared with the filter, the interceptor also needs to be injected in springmvc.
So we open the startup class and write the following code

 

Here I am /Handlesintercepting in this url After
writing the code, let's see the result

When entering the specified url, we have executed the interceptor and
then we can use the interceptor according to our needs.

the difference

Filters and interceptors are very similar, but they have a big difference.
The most simple and clear difference is that filters can modify requests, while interceptors cannot.
Filters need to be implemented in the servlet container, and interceptors can be applied to javaEE, javaSE, etc. This kind of environment
interceptor can call various dependencies in the IOC container, while the filter cannot be
used before and after the request, and the interceptor can detail each method.
There are many differences, you can check it out

In general
, the filter is to filter out the things you want, such as the part you want in the request. The
interceptor is used more for security, such as terminating some processes
. There is a picture on the Internet that is very good, and it is copied here for everyone to see. one time

The above is the implementation and difference between the filter and the interceptor in springboot;
if you think it's okay, please like it, if you can't like it, you can also bookmark it; in
short, thank you for reading~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324434978&siteId=291194637