Integrate JWT in SpringBoot to realize Token verification (Additional notes)
In the last article, we have integrated JWT in SpringBoot and implemented Token verification. In actual applications, we will find that if each view layer (controller) manually verifies the token, the code will be particularly bloated. This article The article is mainly to solve this problem.
If you are not familiar with the integration of JWT, you can take a look at my previous article: Integrate JWT in SpringBoot to achieve Token verification (Integration)
Create custom annotations
We have created an annotation named JwtToken, which has no parameters. An explanation for some of the above annotations:
- @Target(ElementType.METHOD), Target indicates the scope of the object modified by Annotation, and METHOD is used to describe the method
- @Retention(RetentionPolicy.RUNTIME), runtime annotation, the annotation is not only saved in the class file but still exists after the JVM loads the class file
- @Documented, meta-annotation, indicating that this annotation should be recorded by the Javadoc tool
We intercept all request paths starting with api, and if it is annotated with @JwtToken, token information will be verified, thus realizing a custom annotation verification token.