在用spring mvc提供的拦截器做登录拦截的时候,我们经常需要编写自定义的登录拦截器,经常会发现拦截器无法使用或者是exclude-mapping无法起作用,总是被拦截,其原因主要是由于静态资源文件被拦截的原因,chrome浏览器中按f12,在network会发现js文件或者css文件等报红色的情况,这个时候虽然在spring-servlet里面配置了类似下面的资源
<mvc:resources location="/pages/" mapping="/pages/**"/>
<mvc:resources location="/styles/" mapping="/styles/**"/>
<mvc:resources location="/scripts/" mapping="/scripts/**"/>
mvc:interceptors>
<!-- 配置登录拦截 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/auth/**"/>
<bean class="com.ctrip.sp.cs.intercepter.LoginIntercepter"/>
</mvc:interceptor>
</mvc:interceptors>
但是貌似静态资源的配置不会起作用,这个时候我们还是需要将resource文件加入到拦截器例外exclude-mapping中来:修改后如下:<mvc:resources location="/pages/" mapping="/pages/**"/>
<mvc:resources location="/styles/" mapping="/styles/**"/>
<mvc:resources location="/scripts/" mapping="/scripts/**"/>
<mvc:interceptors>
<!-- 配置登录拦截 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/auth/**"/>
<mvc:exclude-mapping path="/pages/**"/>
<mvc:exclude-mapping path="/styles/**"/>
<mvc:exclude-mapping path="/scripts/**"/>
<bean class="com.ctrip.sp.cs.intercepter.LoginIntercepter"/>
</mvc:interceptor>
</mvc:interceptors>
这个时候就会发现可以正常使用了,js也不会被拦截了。