服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Springmvc拦截器执行顺序及各方法作用详解

Springmvc拦截器执行顺序及各方法作用详解

2021-05-20 14:13开拖拉机的蜡笔小新 Java教程

这篇文章主要介绍了Springmvc的拦截器执行顺序及各方法作用,下面讲实现其接口的写法,先看一下这个接口的三个方法,需要的朋友可以参考下

实现handlerinterceptor接口或者继承handlerinterceptor的子类,比如spring 已经提供的实现了handlerinterceptor 接口的抽象类handlerinterceptoradapter ,下面讲实现其接口的写法,先看一下这个接口的三个方法.

- 方法prehandle: 顾名思义,该方法将在请求处理之前进行调用,在controller之前执行。springmvc 中的interceptor 是链式的调用的,在一个应用中或者说是在一个请求中可以同时存在多个interceptor 。每个interceptor 的调用会依据它的声明顺序依次执行,而且最先执行的都是interceptor 中的prehandle 方法,所以可以在这个方法中进行一些前置初始化操作或者是对当前请求的一个预处理,比如说获取cookie的值或者判断是否已经登录,也可以在这个方法中进行一些判断来决定请求是否要继续进行下去。该方法的返回值是布尔值boolean 类型的,当它返回为false 时,表示请求结束,后续的interceptor 和controller 都不会再执行;当返回值为true 时就会继续调用下一个interceptor 的prehandle 方法,如果已经是最后一个interceptor 的时候就会是调用当前请求的controller 方法。

- 方法posthandle:由prehandle 方法的解释我们知道这个方法包括后面要说到的aftercompletion 方法都只能是在当前所属的interceptor 的prehandle 方法的返回值为true 时才能被调用。posthandle 方法,顾名思义就是在当前请求进行处理之后,也就是controller 方法调用之后执行,但是它会在dispatcherservlet 进行视图返回渲染之前被调用,所以我们可以在这个方法中对controller 处理之后的modelandview 对象进行操作,比如说设置cookie,返回给前端。posthandle 方法被调用的方向跟prehandle 是相反的,也就是说先声明的interceptor 的posthandle 方法反而会后执行

- 方法aftercompletion:该方法也是需要当前对应的interceptor 的prehandle 方法的返回值为true 时才会执行。顾名思义,该方法将在整个请求结束之后,也就是在dispatcherservlet 渲染了对应的视图之后执行。这个方法的主要作用是用于进行资源清理工作的。

例:

?
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@component
public class authinterceptor implements handlerinterceptor {
 private static final string token_cookie = "token";
 @autowired
 private userdao userdao;
 @override
 public boolean prehandle(httpservletrequest req, httpservletresponse res, object handler)
     throws exception {
  map<string, string[]> map = req.getparametermap();
  map.foreach((k,v) ->req.setattribute(k, joiner.on(",").join(v)));
  string requesturi = req.getrequesturi();
  if (requesturi.startswith("/static") || requesturi.startswith("/error")) {
   return true;
  }
  cookie cookie = webutils.getcookie(req, token_cookie);
  if (cookie != null && stringutils.isnoneblank(cookie.getvalue())) {
    user user = userdao.getuserbytoken(cookie.getvalue());
    if (user != null) {
     req.setattribute(commonconstants.login_user_attribute, user);
     usercontext.setuser(user);
    }
  }
  return true;
 }
 @override
 public void posthandle(httpservletrequest req, httpservletresponse res, object handler,
     modelandview modelandview) throws exception {
  string requesturi = req.getrequesturi();
  if (requesturi.startswith("/static") || requesturi.startswith("/error")) {
   return ;
  }
  user user = usercontext.getuser();
  if (user != null && stringutils.isnoneblank(user.gettoken())) {
    string token = requesturi.startswith("logout")? "" : user.gettoken();
    cookie cookie = new cookie(token_cookie, token);
    cookie.setpath("/");
    cookie.sethttponly(false);
    res.addcookie(cookie);
  }
 }
 @override
 public void aftercompletion(httpservletrequest req, httpservletresponse response, object handler, exception ex)
     throws exception {
  usercontext.remove();
 }
}

总结

以上所述是小编给大家介绍的springmvc拦截器执行顺序及各方法作用详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.cnblogs.com/xiangkejin/archive/2018/07/25/9368984.html

延伸 · 阅读

精彩推荐