Nerd For Tech
Published in

Nerd For Tech

Integrate JWT in SpringBoot to realize Token verification (Interceptor)

In the previous article, we have implemented the use of custom annotations to verify token information, so we will find that when we need to verify more interfaces, we need to add the @ JwtToken annotation to each method, which is also very troublesome. In this article, we continue to use interceptors to verify token information.

Photo by AltumCode on Unsplash

If you are not familiar with the integration of JWT, you can take a look at my article: Integrate JWT in SpringBoot to achieve Token verification (Integration)

If you are interested in custom annotation verification token information, you can check out my article: Integrate JWT in SpringBoot to realize Token verification (Additional notes)

Custom interceptor

We verify the token information through the JWT that has been integrated in the previous article. If the verification is successful, return true. If the verification fails, change the code of the HttpServletResponse object to 401, which means that the user is not logged in and return false. At this time, the method will end directly. We go to the next step

Annotation interceptor

By registering our custom interceptor, we set the interception path, and the path starting with api will be verified token information.
We also set the non-interception path, such as registration, login, forget the password, etc. You can directly request without user login, There is no need to verify the token information.

At this point, we have completed the verification of the token information with a custom interceptor. Compared with custom annotations, this method is simpler and more convenient. We only need to pay attention to which paths are blocked and which paths are not blocked.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store