博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring拦截器
阅读量:4228 次
发布时间:2019-05-26

本文共 1145 字,大约阅读时间需要 3 分钟。

spring拦截器实现HandlerInterceptor接口或者继承HandlerInterceptorAdapter类都可以实现,主要有三个方法,preHandle在调用具体的controller之前执行,postHandle在调用controller之后,但是页面渲染之前执行,afterCompletion在页面渲染之后执行,拦截器的功能类似与filter的功能,个人认为spring为什么搞一个拦截器,主要是因为拦截器属于spring管理的范畴类,方便资源的分配,也便于与其他的整合,不会像filter这样突兀,和filter类型,同事命中多个拦截器时,按顺序一个一个往后执行。拦截器中可以做一些权限校验,数据校验转换等事情。

编写拦截器

public class UserContextInterceptor extends HandlerInterceptorAdapter {    //拦截器可以使用spring的依赖注入    @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {        //可以在这里处理用户认证        if (true) {            response.setContentType("application/json");            response.setCharacterEncoding("UTF-8");            try (PrintWriter writer = response.getWriter()) {                writer.write("{'message':'认证不通过'}");            } catch (IOException e) {                e.printStackTrace();            }        }        return super.preHandle(request, response, handler);    }}

配置拦截器

如何配置,比如访问/hello/word.do,就会返回认证不通过

转载地址:http://xhjqi.baihongyu.com/

你可能感兴趣的文章
Expert Oracle9i Database Administration
查看>>
.NET Framework Solutions: In Search of the Lost Win32 API
查看>>
Linux for Embedded and Real-Time Applications
查看>>
Refactoring in Large Software Projects : Performing Complex Restructurings Successfully
查看>>
Semantic Web Technologies : Trends and Research in Ontology-based Systems
查看>>
Linux All-in-One Desk Reference For Dummies
查看>>
Security Patterns : Integrating Security and Systems Engineering
查看>>
Cross-Platform Web Services Using C# & JAVA
查看>>
UML Xtra-Light: How to Specify Your Software Requirements
查看>>
Fundamentals of OOP and Data Structures in Java
查看>>
C++ Network Programming, Vol. 1: Mastering Complexity with ACE and Patterns
查看>>
The Ethical Hack: A Framework for Business Value Penetration Testing
查看>>
Agile Development with ICONIX Process: People, Process, and Pragmatism
查看>>
Practical Guide to Software Quality Management
查看>>
Real Process Improvement Using the CMMI
查看>>
GDI+ Programming in C# and VB .NET
查看>>
Implementing and Integrating Product Data Management and Software Configuration Management
查看>>
Software Configuration Management
查看>>
Agile Project Management: How to Succeed in the Face of Changing Project Requirements
查看>>
MySQL Bible
查看>>