SpringBoot自定义拦截器实现
1、编写拦截器实现类,此类必须实现接口 HandlerInterceptor,然后重写里面需要的三个比较常用的方法,实现自己的业务逻辑代码
如:OneInterceptor
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
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 所拦截到。
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自定义拦截器实现IP白名单功能
SpringBoot自定义拦截器实现IP白名单功能 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8993331.html 首先,相关功能已经上线了,且先让我先 ...
- SpringMVC拦截器与SpringBoot自定义拦截器
首先我们先回顾一下传统拦截器的写法: 第一步创建一个类实现HandlerInterceptor接口,重写接口的方法. 第二步在XML中进行如下配置,就可以实现自定义拦截器了 SpringBoot实现自 ...
- SpringBoot(11) SpringBoot自定义拦截器
自定义拦截器共两步:第一:注册.第二:定义拦截器. 一.注册 @Configuration 继承WebMvcConfigurationAdapter(SpringBoot2.X之前旧版本) 旧版本代码 ...
- springboot 2.0+ 自定义拦截器
之前项目的springboot自定义拦截器使用的是继承WebMvcConfigurerAdapter重写常用方法的方式来实现的. 以下WebMvcConfigurerAdapter 比较常用的重写接口 ...
- SpringBoot学习笔记:自定义拦截器
SpringBoot学习笔记:自定义拦截器 快速开始 拦截器类似于过滤器,但是拦截器提供更精细的的控制能力,它可以在一个请求过程中的两个节点进行拦截: 在请求发送到Controller之前 在响应发送 ...
- 【学习】SpringBoot之自定义拦截器
/** * 自定义拦截器 **/ @Configuration//声明这是一个拦截器 public class MyInterceptor extends WebMvcConfigurerAdapte ...
- SpringBoot之自定义拦截器
一.自定义拦截器实现步骤 1.创建拦截器类并实现HandlerInterceptor接口 2.创建SpringMVC自定义配置类,实现WebMvcConfigurer接口中addInterceptor ...
- springboot实现自定义拦截器
为了更容易理解,我们通过一个代码例子来演示. 例子: 我们现在要访问http://localhost:8080/main.html页面,这个页面需要登录之后才能够浏览,没登录不能浏览. 那么现在问题来 ...
- SpringBoot 2.x 自定义拦截器并解决静态资源访问被拦截问题
自定义拦截器 /** * UserSecurityInterceptor * Created with IntelliJ IDEA. * Author: yangyongkang * Date: ...
随机推荐
- JDBC查询实例
作为Java与数据库交互最古老的.最基础的规范,JDBC提供了访问底层数据库的接口,其他ORM框架都是在JDBC这块基石上构建的.下面我们看一个基本的JDBC查询例子: package com.ins ...
- redis+php实现微博功能(一)
(一).微博功能概况 微博用户账号注册 微博用户登录 微博发布 添加微博好友(粉丝) 微博推送 微博冷数据写入mysql数据库 (二).redis数据结构设计 这节分享微博用户注册与登录:我们完全采用 ...
- laravel里面使用event
模式:大概是通过一个自定义的event,一个handler,还有一个binder,然后用来简化通知模型 生成自定义的event ./artisan make:event MyEvent 生成自定义的h ...
- 兼顾FPGA和单片机
当今,FPGA和单片机的应用非常广泛,在有FPGA知识的基础上,笔者认为可以把FPGA作为主要的学习目标,单片机作为辅助技能.其实二者是相互促进的.不管是FPGA还是单片机的开发其实都是基于较强的逻辑 ...
- Nginx启停
启动nginx /usr/local/nginx/nginx #不指定配置文件地址/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx. ...
- 简单租房子实例详解---(session、ajax、json前后台数据处理、分页)
本次实例我们结合session.ajax.json前后台数据处理.分页技术做一个租房信息系统 一共有五个界面:包括 管理员和用户的登录界面 登录界面的后台 <?php session_start ...
- yum问题的解决办法
关于使用yum“The program package-cleanup is...”的解决办法 在使用yum 时总是有提示信息: The program package-cleanup is f ...
- litecoin源码
安装钱包 http://www.laiteb.com/92 莱特币基于比特币的v0.9.0rc2修改而来,从比较两者最新源码的分支图可以看出
- python开发_自己开发的一个小游戏
先看看游戏的运行效果: 看完游戏的运行情况,你可能对游戏有了一定了了解: #运行游戏后,玩家首先要进行语音的选择,1选择英语,2选择汉语,其他则默认选择英语 #根据玩家选择的语音,进入不同的语音环境 ...
- ie6绝对定位的块会被select元素遮挡的解决方案
RT(已无力吐槽ie),解决方法是:定义一个iframe,与想要显示的绝对定位的块设置为同一大小.放在同一个位置上.我的网页里绝对定位的元素是会随着鼠标移动显示和隐藏的,于是这个frame也要跟着显示 ...