Solve the pit of path path of SpringMVC interceptor

  • 2021-12-05 06:05:01
  • OfStack

Directory SpringMVC Interceptor path Path Pit SpringMVC Interceptor Set Multiple path Set Multiple path

Pit of path Path of SpringMVC Interceptor

SpringMVC provides a very convenient interceptor for us to develop and use. In the configuration file through < mvc:mapping path="" / > Configure the path of interception, but this path has a small pit when configuring

As we all know, /** in the SpringMVC interceptor is to configure all paths. Now I have a requirement to intercept requests starting with /user/. My path is written as /user/**, so that all requests with symbolic conditions can be intercepted.

Then my project requirements increased, and I added multiple access control to the project and wanted to use an interceptor to control it. Suppose there is a function now that is only open to administrators, so I set its access path to/user/manager/xxxx/xxxx. So I added another interceptor, hoping to intercept all requests that have /manager/in the path. I wrote path to/manager/' **, but it didn't work.

The interceptor of user is normal, but the interceptor of manager is not working. I thought that there was something wrong with the interceptor. Then after checking for one afternoon, I found that I changed the path to/manager/user/xxxx/xxxx to manager interceptor working but user interceptor not working. I was sure that there was something wrong with path.

After one modification attempt, I see, Originally, /user/** only applies to requests beginning with /user/, There can be nothing in front of/user/, Since all my requests begin with/user/, Therefore, the interceptor 1 of user can run normally, But the manager interceptor is different. My path is written as /manager/**, but all my requests with /manager/are preceded by /user/, so this does not satisfy /manager/** in the path path. When you want to precede this name with another name, you need /*/manager/**. If there are multiple names preceded, it is /**/manager/**. Of course, since /** is a wildcard, url starting with manager also meets this condition.

The SpringMVC interceptor sets up multiple path

Setting multiple path

If you want to specify multiple path in the same interceptor, add multiple path directly and sequentially < mvc:mapping path="" / > You can:


<!--  Interceptor  -->
<mvc:interceptors>
 <!--  Multiple interceptors , Sequential execution  -->
 <!--  If you do not configure or /**, Will intercept all Controller -->
 <!--  Note that when matching any address, note that 2 A " * "No. is not 1 A " * "  -->
 <mvc:interceptor>
  <mvc:mapping path="/aaa/**" />
  <mvc:mapping path="/bbb/**" />
  <bean class="com.test.blog.interceptor.AccessInterceptor">
   <property name="protectedUrls">
    <list>
     <value>edit</value>
     <value>add</value>
    </list>
   </property>
  </bean>
 </mvc:interceptor>
</mvc:interceptors>

Related articles: