springboot的拦截器和过滤器实现
程序员文章站
2022-09-04 11:49:49
springboot拦截器 说明:和springmvc一样去实现HandlerInterceptor接口 import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerIn ......
- springboot拦截器
说明:和springmvc一样去实现handlerinterceptor接口
import org.springframework.stereotype.component; import org.springframework.web.servlet.handlerinterceptor; import org.springframework.web.servlet.modelandview; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; /** * 〈一句话功能简述〉<br> * todo(参数拦截器1) * * @author sunhan * @version 1.0.0 * @since 2020/3/7 10:06 pm */ @component public class parameterinterceptor1 implements handlerinterceptor { @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception { system.out.println("进入了拦截器-----11111"); return true; } @override public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) { } @override public void aftercompletion(httpservletrequest request, httpservletresponse response, object handler, exception ex) { } }
把自定义拦截器注册到webmvcconfigurer配置接口中:
重写addinterceptors方法,添加自定义拦截器的order,pathpatterns
import org.springframework.context.annotation.configuration; import org.springframework.web.servlet.config.annotation.interceptorregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigurer; /** * 〈一句话功能简述〉<br> * todo() * * @author sunhan * @version 1.0.0 * @since 2020/3/7 10:25 pm */ @configuration public class interceptconfiguration implements webmvcconfigurer { @override public void addinterceptors(interceptorregistry registry) { registry.addinterceptor(new parameterinterceptor1()).addpathpatterns("/get*"); registry.addinterceptor(new parameterinterceptor2()).addpathpatterns("/save*"); } }
- springboot过滤器
说明:注解方式和注册方式
1、注解方式:
import org.springframework.core.annotation.order; import javax.servlet.*; import javax.servlet.annotation.webfilter; import java.io.ioexception; /** * 〈一句话功能简述〉<br> * todo() * * @author sunhan * @version 1.0.0 * @since 2020/3/6 5:28 pm */ @webfilter(filtername = "requestparamfilter",urlpatterns = "/*") @order(3) public class requestparamfilter implements filter { @override public void init(filterconfig filterconfig) throws servletexception { } @override public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception, servletexception { system.out.println("sunhan---请求参数过滤器!---test1"); filterchain.dofilter(servletrequest,servletresponse); } @override public void destroy() { } }
注意:
1.1、注解方式需要@webfilter和@order放在filter实现类上。
1.2、注解方式需要@servletcomponentscan放在springbootapplication启动类上
2、注册方式:
import javax.servlet.*; import javax.servlet.http.httpservletrequest; import java.io.ioexception; public class myfilter implements filter { @override public void init(filterconfig arg0) throws servletexception { } @override public void dofilter(servletrequest srequest, servletresponse sresponse, filterchain filterchain) throws ioexception, servletexception { httpservletrequest request = (httpservletrequest) srequest; system.out.println("this is myfilter,url :"+request.getrequesturi()); filterchain.dofilter(srequest, sresponse); } @override public void destroy() { } }
注意:@bean放在一个方法中,然后通过springboot的filterregistrationbean注册到过滤链中
import org.springframework.boot.web.servlet.filterregistrationbean; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; @configuration public class webconfiguration { @bean public filterregistrationbean testfilterregistration() { filterregistrationbean registration = new filterregistrationbean(); registration.setfilter(new myfilter()); registration.addurlpatterns("/*"); registration.setname("myfilter"); registration.setorder(6); return registration; } @bean public filterregistrationbean test2filterregistration() { filterregistrationbean registration = new filterregistrationbean(); registration.setfilter(new myfilter2()); registration.addurlpatterns("/*"); registration.setname("myfilter2"); registration.setorder(1); return registration; } }
上一篇: 春季补水 南北方白领各有侧重点