SpringBoot2 添加应用拦截器
项目参考:详细参见:《Spring Boot 2精髓:从构建小系统到架构分布式大系统》 第三章 3.6.1节 拦截器

MyWebMvcConfigurer
package com.archibladwitwicke.springboot2.chapter03.configurer; import com.archibladwitwicke.springboot2.chapter03.intercept.AdminLoginIntercept;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer { @Override
public void addInterceptors(InterceptorRegistry registry) {
// 添加一个拦截器,连接以/admin为前缀的 url路径
registry.addInterceptor(new AdminLoginIntercept()).addPathPatterns("/admin/**");
}
}
TestAdminController
package com.archibladwitwicke.springboot2.chapter03.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/admin")
public class TestAdminController { @RequestMapping("/hello")
@ResponseBody
public String say() {
return "this is a admin page.";
}
}
AdminLoginIntecept
package com.archibladwitwicke.springboot2.chapter03.intercept; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class AdminLoginIntercept implements HandlerInterceptor { @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 如果已经登录返回true。
// 如果没有登录没有登录,可以使用 reponse.send() 跳转页面。后面要跟return false,否则无法结束; // 为了测试,打印一句话
System.out.println("访问了admin下url路径。"); return true;
} @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
}
SpringBoot2 添加应用拦截器的更多相关文章
- Vue添加请求拦截器
一.现象 统一处理错误及配置请求信息 二.解决 1.安装 axios , 命令: npm install axios --save-dev 2.在根目录的config目录下新建文件 axios.js ...
- [代码笔记]VUE路由根据返回状态判断添加响应拦截器
//返回状态判断(添加响应拦截器) Axios.interceptors.response.use( res => { //对响应数据做些事 if (res.data && !r ...
- SpringBoot2.X自定义拦截器实战及新旧配置对比(核心知识)
简介: 讲解拦截器使用,Spingboot2.x新版本配置拦截拦截器和旧版本SpringBoot配置拦截器区别讲解 1.@Configuration 继承WebMvcConfigurationAdap ...
- 在SpringBoot项目中添加SpringMVC拦截器
1.认识拦截器 SpringMVC的拦截器(Interceptor)不是Filer,同样可以实现请求的预处理.后处理.使用拦截器仅需要两个步骤 实现拦截器 注册拦截器 1.1实现拦截器 实现拦截器可以 ...
- [原创]java WEB学习笔记65:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) ModelDriven拦截器 paramter 拦截器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- springboot2.0+ 使用拦截器导致静态资源被拦截
在spring1.0+的版本中,配置拦截器后是不会拦截静态资源的.其配置如下: @Configuration public class WebMvcConfig extends WebMvcConfi ...
- 为webService添加Interceptor(拦截器)
今天写一个简单的拦截器,以webService接口为例: 背景:H5的一个项目,只要调用H5webService 接口下面的方法都会触发一个AuthorityInterceptor去验证是否调用类型是 ...
- flask为多个接口添加同一个拦截器的方法
前言 最近又抽掉出来写一个 Python 项目, 框架使用 Flask , 又有些新心得, 比如本篇所说, 想要将某个蓝图加上统一的权限控制, 比如 admin 蓝图全部有一个统一的拦截器判断是否有权 ...
- spring boot 添加拦截器
构建一个spring boot项目. 添加拦截器需要添加一个configuration @Configuration @ComponentScan(basePackageClasses = Appli ...
随机推荐
- jmeter运行时间越久发送请求越来越少
displayed. Size: 412152 > 204800,而且每次点击查看“察看结果树”后会导致jmeter卡死, 解决方法: step1.在user.property中增加 view. ...
- Talend 将Oracle中数据导入到hive中,根据系统时间设置hive分区字段
首先,概览下任务图: 流程是,先用tHDFSDelete将hdfs上的文件删除掉,然后将oracle中的机构表中的数据导入到HDFS中:建立hive连接->hive建表->tJava获取系 ...
- 安装 Express
首先假定你已经安装了 Node.js,接下来为你的应用创建一个目录,然后进入此目录并将其作为当前工作目录. $ mkdir myapp $ cd myapp 通过 npm init 命令为你的应用创建 ...
- 使用vlc实现视频TS流的推送
鉴于Mpeg TS流播放的需求,使用 VLC作为Server来实现输出Mpeg TS 本文仅涉及如何使用VLC的Command来实现作为视频流Server通常可以使用下述四种方式来推送Mpeg ...
- STL学习笔记— —无序容器(Unordered Container)
简单介绍 在头文件<unordered_set>和<unordered_map> 中定义 namespace std { template <typename T, ty ...
- 菜鸟学Java(五)——JSP内置对象之request
书接上回,上次跟大家概括的说了说JSP的九种常用内置对象.接下来就该聊聊它们各自的特点了,今天先说说request吧. 下面是request的一些常用方法: isUserInRole(String r ...
- [Windows Azure] How to use the Table Storage Service
How to use the Table Storage Service version 1.7 version 2.0 This guide will show you how to perform ...
- 运行Virtualbox去安装系统时出错:Failed to open a session for the virtual machine,Unable to load R3 module xxxx/VBoxDD.DLL(VBoxDD)
貌似看到不少人(1,2,3),在使用Virtualbox去安装系统时,都遇到这个错误: 比如这里遇到的: 截图: 和另外这里的截图: 加文字描述: Failed to open a session f ...
- 死亡之Makefile。。。
A=Nothing build: @rm -rf build/$(A)/* > /dev/null .PHONY: build 这是一个Makefile..只需要打开终端,在这个Makefile ...
- JAVA-JSP内置对象之request获得参数的参数值(一个值)
相关资料:<21天学通Java Web开发> 获得参数的参数值(一个值) RequestForm3.jsp <%@ page language="java" co ...