SpringBoot自定义拦截器实现
2018-11-06 15:41
302 查看
1、编写拦截器实现类,此类必须实现接口 HandlerInterceptor,然后重写里面需要的三个比较常用的方法,实现自己的业务逻辑代码
如:OneInterceptor
[code]package com.leecx.interceptors.interceptor; import com.leecx.pojo.LeeJSONResult; import com.leecx.utils.JsonUtils; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; public class OneInterceptor implements HandlerInterceptor{ /** * 在请求处理之前进行调用(Controller方法调用之前) */ @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception { System.out.println("====================="); if (true) { returnErrorResponse(httpServletResponse, LeeJSONResult.errorMsg("被one拦截...")); } System.out.println("被one拦截..."); return false; } /** * 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后) */ @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } /** * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作) */ @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { } public void returnErrorResponse(HttpServletResponse response, LeeJSONResult result) throws IOException, UnsupportedEncodingException { OutputStream out = null; try{ response.setCharacterEncoding("utf-8"); response.setContentType("text/json"); out = response.getOutputStream(); out.write(JsonUtils.objectToJson(result).getBytes("utf-8")); out.flush(); } finally{ if(out!=null){ out.close(); } } } }
说明:
1、preHandle 方法会在请求处理之前进行调用(Controller方法调用之前)
2、postHandle 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
3、afterCompletion 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
2、编写拦截器配置文件主类 WebMvcConfigurer 此类必须继承 WebMvcConfigurerAdapter 类,并重写其中的方法 addInterceptors 并且在主类上加上注解 @Configuration
如:WebMvcConfigurer
[code]package com.leecx.interceptors.config; import com.leecx.interceptors.interceptor.OneInterceptor; import com.leecx.interceptors.interceptor.TwoInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebMvcConfigurer extends WebMvcConfigurerAdapter{ /** * <p>Title:</p> * <p>Description:重写增加自定义拦截器的注册,某一个拦截器需要先注册进来,才能工作</p> * param[1]: null * return: * exception: * date:2018/4/18 0018 下午 17:29 * author:段美林[duanml@neusoft.com] */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new OneInterceptor()).addPathPatterns("/one/**"); registry.addInterceptor(new TwoInterceptor()).addPathPatterns("/one/**") .addPathPatterns("/two/**"); super.addInterceptors(registry); } }
说明:
拦截器的执行是会根据 registry 注入的先后顺序执行,比如:/one/** 同时被 OneInterceptor、TwoInterceptor 拦截,但会先执行 OneInterceptor拦截的业务请求,因为它先注入进来的
3、在controller业务层,只要请求的地址为 拦截器 申明的需要拦截地址即可进入相应的业务处理。
如:OneInterceptorController 这个controller 类的所有实现方法都会被 拦截器 OneInterceptor 所拦截到。
[code]package com.leecx.controller; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import com.leecx.pojo.User; @Controller @RequestMapping("/one") public class OneInterceptorController { @RequestMapping("/index") public String index(ModelMap map) { System.out.println("=========one is index"); map.addAttribute("name", "itzixi22"); return "thymeleaf/index"; } @RequestMapping("center") public String center() { return "thymeleaf/center/center"; } @RequestMapping("test") public String test(ModelMap map) { User user = new User(); user.setAge(18); user.setName("manager"); user.setPassword("123456"); user.setBirthday(new Date()); map.addAttribute("user", user); User u1 = new User(); u1.setAge(19); u1.setName("itzixi"); u1.setPassword("123456"); u1.setBirthday(new Date()); User u2 = new User(); u2.setAge(17); u2.setName("LeeCX"); u2.setPassword("123456"); u2.setBirthday(new Date()); List<User> userList = new ArrayList<>(); userList.add(user); userList.add(u1); userList.add(u2); map.addAttribute("userList", userList); return "thymeleaf/test"; } @PostMapping("postform") public String postform(User user) { System.out.println(user.getName()); return "redirect:/th/test"; } }
阅读更多
相关文章推荐
- SpringBoot添加自定义拦截器的实现代码
- SpringBoot使用自定义注解+拦截器 实现日志记录
- 实现SpringBoot自定义拦截器
- springboot 、springmvc 预防xss 攻击 自定义WebBindingInitializer 实现类
- SpringBoot中利用AOP实现拦截器效果
- spring自定义注解实现拦截器的实现方法
- springboot利用springsession实现redis共享session,并且自定义sessionid
- Spring Boot使用过滤器和拦截器分别实现REST接口简易安全认证
- spring boot实现过滤器和拦截器demo
- Spring Boot 自动配置理解 以及实现自定义Starter
- Spring Boot学习之路——自定义拦截器
- spring-boot实现增加自定义filter(新)
- [SpringBoot实践]spring-data-mongo自定义Repository接口及其实现
- Springboot+Mybatis+Jquery实现自定义二级联动
- spring boot 使用spring AOP实现拦截器
- springboot过滤器和拦截器的实现和区别
- spring boot 拦截器的实现以及遇到的问题的解决方案
- SpringBoot中实现拦截器, 并实现对404和500等错误的拦截。
- Spring boot自定义注解拦截器
- SpringBoot 实现Freemarker自定义标签