How spring boot adds interceptors

Build a spring boot project.

Adding an interceptor requires adding a configuration

To facilitate the scanning location, we can write an interface or entry class application and place it in the outermost package, which will scan the classes of this class and its sub packages.

1. Resources configuration

When this class is not configured, we can Modify static file location and matching method in YM:

However, after we inherit webmvcconfigurationsupport and scan the configuration, the configuration of the above resources becomes invalid and the default configuration is restored. Then we need to specify the static resource location again in this class:

In this way, the URL to access the static resources under the static package under the classpath matches / static / xxx js。 The URL of the static file under the default matching static is / xxx JS, although clean, I don't think idea can recognize this path. It's better to change it to a complete path.

2. Interceptor configuration

Configure login interception or something else. You need to create an interceptor class to inherit handlerinterceptoradapter, and then just overwrite the location you want to intercept. For example, before I intercept the access method:

After writing the interceptor, you need to add this interceptor to the servletcontextconfig you started to create:

The complete servletcontextconfig is:

GitHub address: https://github.com/chenxing12/spring-boot-demo

The demo source code: spring boot demo_ jb51. rar

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>