Example explanation of string boot and custom interceptor
I've learned about filters before, but the filters are for servlets. They are used in spring MVC and spring boot. In terms of functions, they don't feel very easy to use
Let's learn about interceptors
I Execution sequence of interceptors
1. Contents
2. Interceptor
In the interceptor, I added three (first, two, third), but the content is almost the same
Only when prehandle returns true will the following execution continue
Interceptor registration:
In order to verify the execution order, we use thymeleaf, and then access the properties passed in the background in the foreground. When accessing, the information will be printed to the console
Controller:
View:
When accessing localhost: 8080 / first / index, the response information will be output on the console
In this way, we can see the execution sequence of a single interceptor
1. Execute the prehandle method before the controller method is executed
2. Execute the action method of the controller
3. After executing the action, execute the posthandle method of the interceptor before parsing the view (if any)
4. Parse view
5. After parsing, execute the aftercompletion method
When registering multiple interceptors, the execution sequence is shown in the figure
II The interceptor implements permission verification
Similarly, add a permission interceptor first
Then register the permission interceptor
Add a login controller to the controller to provide login page and logout method
Here I made a login free annotation, which can be added to the controller or action
There is no need for anything in the annotation
Login page (the login page here is only for logout, so after visiting this page, it means that the login is successful)
Result presentation method:
In the browser, open first http://localhost:8080/login/index Page, and then access it in a new tab http://localhost:8080/first/index Page
You will find that you can access first / index
At this time, click the logout button on the login / index page, and then refresh the first / index page to directly jump to the login page
The above example explanation of string boot and custom interceptor is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.