美文网首页
Spring Interceptor

Spring Interceptor

作者: Draper | 来源:发表于2018-11-07 15:15 被阅读0次

    Spring中使用Interceptor拦截器

    与 Servlet 的 listener 比较相似,同样是监听请求,同样需要配置,一个是在 web.xml 中,Spring 拦截器是在 Spring 配置文件中

    • 拦截未登录用户访问某些连接
    • 拦截日志信息
    • 拦截 SQL 注入

    执行顺序是 preHandle -> controller -> postHandle -> afterCompletion

    Spring Interceptor 属于链式调用,可以同时有多个 Interceptor,且在运行时都会先执行 preHandle

    preHandle 的返回值为 boolean 值,当返回值为 true 时,将会继续调用其他 Interceptor,反之 preHandler 和 controller afterCompletion 都会停止。

    postHandler 会在 Dispatcher 进行视图渲染前执行
    先声明的 Interceptor 在 postHandler 最后执行

    afterCompletion 则会在 Dispatcher 渲染后进行调用,一般用来做垃圾处理工作。

    相关文章

      网友评论

          本文标题:Spring Interceptor

          本文链接:https://www.haomeiwen.com/subject/vqhexqtx.html