你首先需要一个搭建好的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. 開始搭建第一个zookeeper

    首先须要下载zookeeper的tar包.地址为http://zookeeper.apache.org,然后再linux中解压并编译tar包. # tar-xvzf zookeeper-3.4.5.t ...

  2. strcpy函数使用方法以及底层实现

    strcpy(s1, s2);   strcpy函数的意思是:把字符串s2中的内容copy到s1中.连字符串结束标志也一起copy. 这样s1在内存中的存放为:ch\0; 在cout<<s ...

  3. 基于spark1.4的Spark-Sql

    Author: kwu 基于spark1.4的Spark-Sql,spark1.4.1在7月15刚公布.提供较好sql支持 1.怎样启动Spark-Sql 启动脚本例如以下 #!/usr/bin/en ...

  4. C++派生类中如何初始化基类对象(五段代码)

    今天收到盛大的面试,问我一个问题,关于派生类中如何初始化基类对象,我在想派生类对于构造函数不都是先构造基类对象,然后在构造子类对象,但是如果我们在成员初始化列表先初始化派生类的私有成员,在函数内去调用 ...

  5. [JavaEE] Apache Maven 入门篇(上)

    http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-1-406235-zhs.html 作 ...

  6. vue-阻止事件冒泡-开启右键-键盘类事件

    一: 阻止事件冒泡 布局: 当点击按钮时,会触发button的click 也会触发父级的方法 <div id="box"> <div @click="p ...

  7. C#自定义控件实现控件随窗口大小改变

    1.新建用户控件,取名MyForm. 2.将默认的UserControl改成Form 3.在类中添加以下代码 private float X, Y; //获得控件的长度.宽度.位置.字体大小的数据 p ...

  8. Linux强行踢用户

    首先who执行查看有几个终端在链接使用系统.如要踢出tty2 方法1: pkill -9 -t tty2 方法2: fuser -k /dev/tty2 fuser 指令 用途 使用文件或文件结构识别 ...

  9. html img加载不同大小图像速度

    最近要想法提高网页的性能,在查看图片加载时,产生了试验的想法.一直以来都没有太去深究,还是挖掘一下的好. 很简单的试验,<img>加载两个图像,一个2.3MB,5000*5000,一个22 ...

  10. img-responsive class图片响应式

    在BootStrap中,给<img>添加 .img-responsive样式就可以实现图片响应式. 1 <img src="..." class="im ...