前言
在编写过滤器、监听器、拦截器之前我们需要在spring-boot启动的类上加上注解@ServletComponentScan:
1
2
3
4
5
6
7
|
@SpringBootApplication @ServletComponentScan public class MySpringbootApplication { public static void main(String[] args) { SpringApplication.run(MySpringbootApplication. class , args); } } |
Servlet
spring-boot编写过滤器和spring中差不多,直接看代码:
1
2
3
4
5
6
7
8
9
10
11
12
|
@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-------------" ); } } |
其实也就是注解的不同而已:
1
|
@WebServlet (urlPatterns = "/serv" ) |
过滤器(Filter)
在spring-boot里编写过滤器我们只需要实现javax.servlet.Filter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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( "销毁过滤器!" ); } } |
然后添加一个注解:
1
|
@WebFilter (filterName = "myFilter" , urlPatterns = "/*" ) |
监听器 (Listener)
在上面,看了下过滤器的使用。其实监听器和拦截器就差不多了,直接上代码:
1
2
3
4
5
6
7
8
9
10
11
|
@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),并在方法中添加自己定义的拦截器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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中如何定义过滤器、监听器和拦截器。关于他们的原理以及一些细节问题(如拦截器的拦截顺序),就不详述。有兴趣的可以去网上搜索。
原文链接:http://blog.csdn.net/qq_31084201/article/details/74626667