SpringBoot2.0 基础案例(05):多个拦截器配置和使用场景
程序员文章站
2022-04-29 18:02:58
一、拦截器简介 1、拦截器定义 拦截器,请求的接口被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。 拦截器主要用来按照指定规则拒绝请求。 2、拦截器中应用 二、SpringBoot2.0拦截器用法 1、编写两个拦截器 自定义类实现HandlerInterceptor接口 ......
一、拦截器简介
1、拦截器定义
拦截器,请求的接口被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是aop的一种实现策略。 拦截器主要用来按照指定规则拒绝请求。
2、拦截器中应用
token令牌验证 请求数据校验 用户权限校验 放行指定接口
二、springboot2.0拦截器用法
1、编写两个拦截器
自定义类实现handlerinterceptor接口
1)oneinterceptor 拦截器
import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.web.servlet.handlerinterceptor; import org.springframework.web.servlet.modelandview; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; /** * 拦截器一 */ public class oneinterceptor implements handlerinterceptor { private static final logger logger = loggerfactory.getlogger(oneinterceptor.class.getname()); @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object o) throws exception { string url =string.valueof(request.getrequesturl()) ; logger.info("1、url=="+url); // 放开拦截 return true; } @override public void posthandle(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, object o, modelandview modelandview) throws exception { logger.info("1、posthandle"); } @override public void aftercompletion(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, object o, exception e) throws exception { logger.info("1、aftercompletion"); } }
2)twointerceptor 拦截器
import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.web.servlet.handlerinterceptor; import org.springframework.web.servlet.modelandview; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; /** * 拦截器二 */ public class twointerceptor implements handlerinterceptor { private static final logger logger = loggerfactory.getlogger(twointerceptor.class.getname()); @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object o) throws exception { string url =string.valueof(request.getrequesturl()) ; logger.info("2、url=="+url); // 放开拦截 return true; } @override public void posthandle(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, object o, modelandview modelandview) throws exception { logger.info("2、posthandle"); } @override public void aftercompletion(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, object o, exception e) throws exception { logger.info("2、aftercompletion"); } }
2、web配置文件中注入拦截器
import com.boot.intercept.intercept.oneinterceptor; import com.boot.intercept.intercept.twointerceptor; import org.springframework.context.annotation.configuration; import org.springframework.web.servlet.config.annotation.interceptorregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigurer; /** * web配置文件 */ @configuration public class webmvcconfig implements webmvcconfigurer { public void addinterceptors(interceptorregistry registry) { // 拦截所有路径 // 注册自定义两个拦截器 registry.addinterceptor(new oneinterceptor()).addpathpatterns("/**"); registry.addinterceptor(new twointerceptor()).addpathpatterns("/**"); } }
3、编写测试接口
import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class interceptcontroller { @requestmapping("/requrl") public string requrl (){ return "success" ; } }
4、访问测试接口
日志输出内容如下
intercept.oneinterceptor : 1、url==http://127.0.0.1:8005/requrl intercept.twointerceptor : 2、url==http://127.0.0.1:8005/requrl intercept.twointerceptor : 2、posthandle intercept.oneinterceptor : 1、posthandle intercept.twointerceptor : 2、aftercompletion intercept.oneinterceptor : 1、aftercompletionla
拦截器的拦截顺序,是按照web配置文件中注入拦截器的顺序执行的。
三、源代码地址
github:知了一笑 https://github.com/cicadasmile/spring-boot-base