SpringMVC mvc:interceptor拦截器配置 浅浅的花香味﹌ 2022-06-17 13:28 232阅读 0赞 List: 1.需求 2.原理实现 3.总结 # ……1.需求…… # 系统间数据调用需要使用token令牌进行握手,如果没有令牌就不能获取数据。后台使用rest开发接口,这样在请求restcontroller的时候,就会有个验证。怎么样在入口就进行拦截呢,系统使用的ssm框架。springmvc有个interceptor的使用。就是将自定义的拦截器挂到拦截器链上。 利用mvc:interceptors标签声明一系列的拦截器,然后它们就可以形成一个拦截器链,拦截器的执行顺序是按声明的先后顺序执行的,先声明的拦截器中的preHandle方法会先执行,然而它的postHandle方法和afterCompletion方法却会后执行。 # ……2.原理实现…… # 使用起来很简单,第一步要定义interceptor的实现类,有两种方法, 第一种方式是要定义的Interceptor类要实现了Spring的HandlerInterceptor 接口 第二种方式是继承实现了HandlerInterceptor接口的类,比如Spring已经提供的实现了HandlerInterceptor接口的抽象类HandlerInterceptorAdapter。 第二步在springmvc配置文件中声明自定义拦截器。 ![20170427104439141][] 我负责的模块是被拦截的,是切面。需要配置一下,就可以了。 <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:exclude-mapping path="/parent/**"/> <bean class="com.hy.authorization.interceptor.SecurityInterceptor" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/parent/**"/> <bean class="com.hy.authorization.interceptor.SecuritySystemInterceptor" /> </mvc:interceptor> </mvc:interceptors> 还有一个问题,是之前学习ssm框架的时候遇到的,就是前台对于静态资源的请求,会被拦截到,这个时候需要定义静态资源不用过滤。 <mvc:resources location="/" mapping="/**/*.js"/> <mvc:resources location="/" mapping="/**/*.css"/> <mvc:resources location="/images/" mapping="/images/*" cache-period="360000"/> # ……3.总结 …… # 学习的内容要运用才行。 [20170427104439141]: /images/20220617/dd7eced9315d441388e369b743d4188b.png
还没有评论,来说两句吧...