springboot中的过滤器拦截器操作与springmvc中的几乎一样所以这里也不过多介绍了,下面举两个简单的栗子演示一下
1 创建过滤器类loginfilter,实现rvlet包下的filter接口(包不要导错),加入注解webfilter
package com.example.filter; import javax.rvlet.*;import javaxperform.rvlet.annotation.webfilter;import javax.rvlet.http.httprvletrequest;import javax.rvlet.http.httprvletrespon;import java.io.ioexception; @webfilter(urlpatterns = "/filter/*")public class loginfilter implements filter { @override public void init(filterconfig filterconfig) throws rvletexception { system.out.println("初始化拦截器"); } @override public void dofilter(rvletrequest rvletrequest, rvletrespon rvletrespon, filterchain filterchain) throws ioexception, rvletexception { system.out.pr双十二什么时候开始的intln("loginfilter"); httprvletrequest request = (httprvletrequest)rvletrequest; httprvletrespon respon = (httprvletrespon)rvletrespon; //放行 filterchain.dofilter(request,respon); } @override public void destroy() { system.out.println("拦截器销毁"); }}
2 创建测试类logincotroller
package com.example.controller; import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.restcontroller; @restcontroller@requestmapping("/filter")public class filtercontroller { @requestmapping("/login") public string login(){ system.out.println("登录"); return "login"; } }
3 在启动类上加注解
package com.example; import org.springframework.boot.springapplication;import org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.boot.web.rvlet.rvletcomponentscan; @springbootapplication@rvletcomponentscan//rvlet、filter、listener可以直接通过@webrvlet、@webfilter、@weblistener注解自动注册public class demoapplication { public static void main(string[] args) { springapplication.run(demoapplication.class, args); } }
4 启动测试,启动的时候可以看到filter调用init方法初始化
接着访问可以看到页面输出
控制台打印出拦截器中的语句
1 创建自定义拦截器
package com.example.interceptor; import org.springframework.web.rvlet.handlerinterceptor;import org.springframework.web.rvlet.modelandview; import javax.rvlet.http.httprvletrequest;import javax.rvlet.http.httprvletrespon; public class myinterceptor implements handlerinterceptor { //进入controller方法之前调用的 @override public boolean prehandle(httprvl少儿编程培训机构etrequest request, httprvletrespon respon, object handler) throws exception { system.out.println("prehandle"); return true;//true表示放行 fal表示不放行 } //调用完controller之同音字组词后,视图渲染层之前 @override public void posthandle(httprvletrequest request, httprvletrespon respon, object handler, modelandview modelandview) throws exception { system.out.println("posthandle"); } //页面跳转之后,整个流程执行之后,一般用于资源的清理 @override public void aftercompletion(httprvletrequest request, httprvletrespon respon, object handler, exception ex) throws exception { system.out.println("aftercompletion"); }}
2 创建拦截器配置类,注意要加上配置类的注解
package com.example.config; import com.example.interceptor.myinterceptor;import org.springframework.context.annotation.configuration;import org.springframework.web.rvlet.config.annotation.interceptorregistry;import org.springframework.web.rvlet.config.annotation.webmvcconfigurer; @configurationpublic class myinterceptorconfig implements webmvcconfigurer { @override public void addinterceptors(interceptorregistry registry) { //设置拦截器并指定拦截路径 registry.addinterceptor(new myinterceptor()).addpathpatterns("/interceptor/*"); //registry.addinterceptor(new myinterceptor()).addpathpatterns("/**");//拦截所有 //registry.addinterceptor(new myinterceptor()).addpathpatterns("/**").excludepathpatterns("/test");//指定不拦截 //添加自定义拦截器 webmvcconfigurer.super.addinterceptors(registry); }}
3 创建logincontroller测试类
package com.example.controller; import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.restcontroller; @restcontroller@requestmapping("/intercep这就是我作文tor")public class interceptorcontroller { @requestmapping("login") public string login(){ system.out.println("interceptor-login"); return "login"; }}
4 运行访问,查看效果
控制台输出如下
到此这篇关于springboot 过滤器与拦截器的文章就介绍到这了,更多相关springboot 过滤器与拦截器内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-03 22:34:19,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/2bbb424a7e977b60bae5af3acd8c1c58.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:SpringBoot 过滤器与拦截器实例演示.doc
本文 PDF 下载地址:SpringBoot 过滤器与拦截器实例演示.pdf
留言与评论(共有 0 条评论) |