你首先需要一个搭建好的springboot项目,具体怎么搭建我还没有相应的随笔可以交给你,可以自己上网上看一下,学习一下,之后我要是总结出来的话,这里面我会通知的

首先这个项目的目录结构是这样子的

首先在Controller里面写上你想要展示的内容

package com.example.springBoot;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class Controller {
@RequestMapping(value="/")
public String Hello(){
return "hello";
}
}

然后自定义一个拦截器

package com.example.springBoot;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class InterceptorUtil implements HandlerInterceptor {
/**
* todo : 在请求处理之前调用,此处当userId==lx时才能正常进入控制器,否则被拦截
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
String userId = httpServletRequest.getParameter("userId");//接收一个userId的参数
if("lx".equals(userId))
return true;
else
return false;
} @Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("执行postHandle方法");
} @Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("执行afterCompletion方法");
}
}
preHandle:请求之前调用,返回值为boolean类型,然后返回true的时候执行下面的两个方法,返回false则不执行
postHandle:请求之后视图渲染之前调用

afterCompletion:视图渲染之后调用

然后写MyWebAppConfigurer,将上述拦截器注入bean中
package com.example.springBoot;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /**
* 注册拦截器
* @author lixue
*
*/
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
@Bean //把拦截器注入为bean
public HandlerInterceptor getMyInterceptor(){
return new InterceptorUtil();
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}

然后运行Application

package com.example.springBoot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }
右键

然后访问

控制台打印了postHandle    afterCompletion这两个方法的输出语句

接下来换一下

什么都没有,控制台也没有继续打印(这是上面操作userId=lx打印出来的userId=lx1并没有打印)

这样就实现了一个简单的拦截器

springboot实现拦截器的更多相关文章

  1. SpringBoot自定义拦截器实现IP白名单功能

    SpringBoot自定义拦截器实现IP白名单功能 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8993331.html 首先,相关功能已经上线了,且先让我先 ...

  2. SpringBoot使用拦截器

    SpringBoot的拦截器只能拦截流经DispatcherServlet的请求,对于自定义的Servlet无法进行拦截. SpringMVC中的拦截器有两种:HandlerInterceptor和W ...

  3. SpringBoot 注册拦截器方式及拦截器如何获取spring bean实例

    SpringBoot 注册拦截器时,如果用New对象的方式的话,如下: private void addTokenForMallInterceptor(InterceptorRegistry regi ...

  4. springboot+springmvc拦截器做登录拦截

    springboot+springmvc拦截器做登录拦截 LoginInterceptor 实现 HandlerInterceptor 接口,自定义拦截器处理方法 LoginConfiguration ...

  5. SpringMVC拦截器与SpringBoot自定义拦截器

    首先我们先回顾一下传统拦截器的写法: 第一步创建一个类实现HandlerInterceptor接口,重写接口的方法. 第二步在XML中进行如下配置,就可以实现自定义拦截器了 SpringBoot实现自 ...

  6. springboot + 注解 + 拦截器 + JWT 实现角色权限控制

    1.关于JWT,参考: (1)10分钟了解JSON Web令牌(JWT) (2)认识JWT (3)基于jwt的token验证 2.JWT的JAVA实现 Java中对JWT的支持可以考虑使用JJWT开源 ...

  7. 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法

    最近使用SpringBoot的自定义拦截器,在拦截器中注入了一个DAO,准备下面作相应操作,拦截器代码: public class TokenInterceptor implements Handle ...

  8. springBoot 使用拦截器 入坑

    近期使用SpringBoot 其中用到了拦截器 结果我的静态资源被全部拦截了,让我导致了好久才搞好: 看下图项目结构: 问题描述:上图划红框的资源都被拦截器给拦截了,搞得项目中不能访问:解决问题就是在 ...

  9. springboot的拦截器Interceptor的性质

    Interceptor在springboot2.x版本的快速入门 实现HandlerInterceptor的接口,并重载它的三个方法:preHandle.postHandle.afterComplet ...

  10. SpringBoot MVC 拦截器

    SpringBoot MVC 环境搭建 在pom.xml添加spring-boot-starter-web   <dependency>   <groupId>org.spri ...

随机推荐

  1. arm32位固定指令中怎么容纳32位变量

    在ARM指令集汇编码中.32位有效马上数是通过______偶数位而间接得到的 A.循环左移 B.循环右移. C.逻辑左移. D.逻辑右移 答案为循环左移.为什么?还有最好解释一下逻辑移动和循环移动的概 ...

  2. 在Mac OS X中部署Tomcat的经验

    因为前几天重装了Mac的系统.准备接下来把一些必需的实验环境都搭建起来.这里简单总结一下在Mac OS X上部署Tomcat应该注意的事情: 下载Tomcat的相应版本号,如http://tomcat ...

  3. 解题报告 之 HDU5303 Delicious Apples

    解题报告 之 HDU5303 Delicious Apples Description There are n apple trees planted along a cyclic road, whi ...

  4. luogu2754 星际转移问题 网络流

    题目大意:地球与月球间有可容纳无限人的太空站,还有在太空站与星球间按周期行驶的.有固定容量的太空船,每一艘太空船从一个太空站驶往任一太空站耗时均为 1.地球上有一定数量的人,问所有人到月球最少需要多少 ...

  5. 网络 - TCP/IP四层模型,面向生产

    TCP.IP四层模型 (网络接口层,网际互联层,传输层,应用层.) (左边是车模,右边是实际生产的车.) 物理层,负责0101比特流传递. 数据链路层,MAC地址负责局域网,内网通信.MAC地址,由4 ...

  6. hihoCoder-1828 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II BFS

    题面 题意:N*M的网格图里,有起点S,终点T,然后有'.'表示一般房间,'#'表示毒气房间,进入毒气房间要消耗一个氧气瓶,而且要多停留一分钟,'B'表示放氧气瓶的房间,每次进入可以获得一个氧气瓶,最 ...

  7. Linux Shell Scripting Cookbook 读书笔记 4

    正则, grep 1. 正则表达式  正则表达式  描述  示例 ^ 行起始标记  ^hell匹配以hell开头的行 $ 行尾标记  test$匹配以test结尾的行 . 匹配任意一个字符  hell ...

  8. 如何用jquery+ajax写省市区的三级联动?(封装和不封装两种方式)-----2017-05-14

    首先,要实现如下图效果, 1.要理清思路: 先做出三个下拉菜单----根据第一个下拉菜单的value值获取第二个下拉列表的内容,第三个同理. 2.用到的数据库表:Chinastates表 规律:根据国 ...

  9. 基于scrapy-redis组件的分布式爬虫

    scrapy-redis组件安装 分布式实现流程 scrapy-redis组件安装 - 下载scrapy-redis组件:pip install scrapy-redis - 更改redis配置文件: ...

  10. javascript实现选项卡切换的4种方法

    方法一:for循环+if判断当前点击与自定义数组是否匹配 <html lang="en"> <head> <meta charset="UT ...