Spring Boot 编写Servlet、Filter、Listener、Interceptor的方法
程序员文章站
2023-11-24 12:49:22
前言
在编写过滤器、监听器、拦截器之前我们需要在spring-boot启动的类上加上注解@servletcomponentscan:
@springbootap...
前言
在编写过滤器、监听器、拦截器之前我们需要在spring-boot启动的类上加上注解@servletcomponentscan:
@springbootapplication @servletcomponentscan public class myspringbootapplication { public static void main(string[] args) { springapplication.run(myspringbootapplication.class, args); } }
servlet
spring-boot编写过滤器和spring中差不多,直接看代码:
@webservlet(urlpatterns = "/serv") public class myservlet extends httpservlet { @override protected void doget(httpservletrequest request, httpservletresponse response) { system.out.println("------------doget-------------"); dopost(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) { system.out.println("------------dopost-------------"); } }
其实也就是注解的不同而已:
@webservlet(urlpatterns = "/serv")
过滤器(filter)
在spring-boot里编写过滤器我们只需要实现javax.servlet.filter
@webfilter(filtername = "myfilter", urlpatterns = "/*") public class myfilter implements filter { @override public void init(filterconfig filterconfig) throws servletexception { system.out.println("初始化过滤器"); } @override public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception { system.out.println("执行过滤器"); filterchain.dofilter(servletrequest, servletresponse); } @override public void destroy() { system.out.println("销毁过滤器!"); } }
然后添加一个注解:
@webfilter(filtername = "myfilter", urlpatterns = "/*")
监听器 (listener)
在上面,看了下过滤器的使用。其实监听器和拦截器就差不多了,直接上代码:
@weblistener public class myhttpsessionlistener implements httpsessionlistener { @override public void sessioncreated(httpsessionevent httpsessionevent) { system.out.println("session 被创建"); } @override public void sessiondestroyed(httpsessionevent httpsessionevent) { system.out.println("session 被摧毁"); } }
我们发现只是注解发生了变化:
@weblistener
拦截器(interceptor)
拦截器大致和上面差不多,不过有一点点不同。我们知道在web开发中,可以使用过滤器和拦截器来过滤外部的web请求。但是拦截器提供了更加细致的控制功能。主要有:请求之前、请求之后渲染之前、渲染之后、请求全部结束之后这四个步骤的拦截。
这里面使用拦截器主要有三个步骤
自定义拦截器,实现org.springframework.web.servlet.handlerinterceptor
自定义webappconfigurer,继承webmvcconfigureradapter
在自定义的webappconfigurer覆盖父类方法addinterceptors(interceptorregistry registry),并在方法中添加自己定义的拦截器
public class myinterceptor implements handlerinterceptor{ @override public boolean prehandle(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, object o) throws exception { system.out.println(myinterceptor.class.getname()+" : 在请求之前调用"); return true; } @override public void posthandle(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, object o, modelandview modelandview) throws exception { system.out.println(myinterceptor.class.getname()+" :请求处理之后视图渲染之前使用"); } @override public void aftercompletion(httpservletrequest httpservletrequest, httpservletresponse httpservletresponse, object o, exception e) throws exception { system.out.println(myinterceptor.class.getname()+" :请视图渲染之后使用"); } } @configuration public class mywebappconfigurer extends webmvcconfigureradapter { @override public void addinterceptors(interceptorregistry registry) { // 多个拦截器组成一个拦截器链 // addpathpatterns 用于添加拦截规则 // excludepathpatterns 用户排除拦截 registry.addinterceptor(new myinterceptor()).addpathpatterns("/**"); registry.addinterceptor(new myinterceptor2()).addpathpatterns("/**"); super.addinterceptors(registry); } }
以上就是关于在spring-boot中如何定义过滤器、监听器和拦截器。关于他们的原理以及一些细节问题(如拦截器的拦截顺序),就不详述。有兴趣的可以去网上搜索。
下一篇: 防止表单重复提交的几种方法总结