Interceptors are particularly familiar to developers who use SpringMvc, Struts, and so on, as they will be used whenever you want to do a good job on a project. Interceptors are used in many ways in our normal projects, such as logging (which we’ll cover in a later section), user logon status interception, security interception, and so on. The internal integration of SpringBoot is the SpringMVC control framework, so it is no different from SpringMVC in use, but slightly different in configuration. Without further ado, let’s begin our chapter!

Objective in this chapter

Learn how to configure interceptors using SpringBoot to perform simple user login status determination.

Build the project

We created a SpringBoot project named Chapter6 and added the following Maven dependencies: Web, MySQL, JPA, Druid, etc. Copy the configuration file application.yml from the previous chapter to the corresponding directory in this chapter. The specific project structure is shown in Figure 1 below:




Figure 1

The content of our application.yml file needs to be modified to add JSP support. In addition, if you download it and put it into your local project, you need to change the database address, user name and password to their own configuration information. Add JSP path configuration support as shown in Figure 2 below:




Figure 2

The SpringBoot project created by InteiiJ IDEA does not have a webApp or WEB-INF structure directory, so we need to add it ourselves, as shown in Figure 3 below:




Figure 3

As you can see in Figure 3 above, we created the corresponding JSP directory. Then we created the JSP page files named index.jsp and login. JSP respectively, and filled in the index.jsp “You have successfully visited the main page! , login.jsp create a login form and enter a username and password field as shown in Figure 4 and Figure 5 below:




Figure 4.




Figure 5

Configuring the Controller

Now that the JSP file has been created, we need to forward the SpringMVC controller in SpringBoot to the corresponding JSP page. We create a class file named LoginController and add the login user’s methods, as shown in Figure 6 below:




Figure 6.

Now we configure the user to read from the database. Because the previous table did not have a “password” field, we add a password field to the previous table as shown in Figure 7 below:




Figure 7.

Table structure has been modified, we create the corresponding entities according to the table structure, according to the entity class directly create the SpringDataJPA management interface, SpringDataJPA specific explanation I leave to the following chapter for you to comb through, this chapter first use the most simple part of the query function. I will directly paste the corresponding entity class code and JPA code, as shown in Figure 8 and 9 below:




Figure 8.





Figure 9.

After the user’s entity and JPA configuration is complete, we open LoginController again, inject UserJPA as a SpringBean, and complete the simple login logic, as shown in Figure 10 below:




Figure 10.

Figure 10 shows that we use SpringDataJPA to query the specified user from the database, and then check whether the password matches. I modify the LoginController again to add the user to the session where the login succeeds. As shown in Figure 11 below:




Figure 11.

So far we haven’t written anything about interceptors, so that’s the foundation, so we’re going to start building.

Creating interceptors

Before creating the interceptor we need to add the forwarding configuration for the initial login page. We create IndexController to add the forwarding configuration for the initial login and the home page, as shown in Figure 12 below:




Figure 12

After that, create an interceptor entity class named SessionInterceptor that implements SpringMVC’s internal interface, HandlerInterceptor. If there is no session status, jump directly to the /user/login_view address, which is our corresponding login.jsp page. The interceptor configuration is shown in Figure 13 below:




Figure 13

Next, we need to add SessionInterceptor interceptor to the SpringBoot configuration, so that the SpringBoot project has such an interceptor. We create a new SessionConfiguration, and configure the interceptor configuration and the interceptor path. As shown in Figure 14 below:




Figure 14

Ok, now that all our work is done, let’s try to run the project for the first time and see if there are any problems

Initial run

We observed InteiilJ IDEA tool console log are shown in figure 15 below, prove your project start there is no problem, then let’s visit the home page address try login: 127.0.0.1:8080 / user/index, we found an unexpected results, shown in the figure 15 below:




Figure 15

Why is this a problem? We have already ruled out /user/login and /user/login_view request addresses why does this still happen? For this problem, go to the SessionInterceptor and print request.getrequesturi () to see the output address, as shown in Figure 16:




Figure 16

We found not only /user/login_view, but also a /error page. Why is this? Then it dawns that we have not configured JSP support in the Pom.xml configuration file. We open the Pom.xml configuration file to add JSP and servlet dependencies, as shown in Figure 17:




Figure 17

Rely on add finished, restart the project, visit 127.0.0.1 again: 8080 / user/index, shows that there is no problem now, we don’t have a login to directly access the index, JSP, will of course be interceptors to intercept, block after the jump straight to the login page (login. JSP), shown in figure 18, the following effects:




Figure 18

Now enter the test user admin and password 123456 in the database to log in, as shown in Figure 19 below:




Figure 19

Show we have login successfully, that is to say our login user information is stored in the session, then we visit 127.0.0.1:8080 / user/index address verification, are shown in figure 20 below:




Figure 20

As you can see in Figure 20, we can now access the contents of the index.jsp page. Our interceptor has done its job perfectly.

conclusion

This is the end of this chapter. The main content of this chapter is to explain how to configure the SpringBoot project interceptor and implement a simple user login status verification interception example.

This chapter code has been uploaded to the code cloud:

SpringBoot source code address: gitee.com/hengboy/spr…

SpringCloud source code address: gitee.com/hengboy/spr…

For SpringBoot related articles, visit: Directory: SpringBoot learning directory

For a series of QueryDSL articles, visit the QueryDSL Generic Query Framework learning directory

For articles in the SpringDataJPA series, visit: Directory: SpringDataJPA Learning directory

SpringBoot related articles please visit: directory: SpringBoot learning directory, thank you for reading!

Welcome to join QQ technical exchange group, common progress.




QQ technical exchange group