SpringBoot拦截器配置
SpringBoot拦截器配置
自定义拦截器:
com.example.mybatis2018.interceptor.MyInterCeptor
package com.example.mybatis2018.interceptor;import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/*** 自定义拦截器*/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class MyInterCeptor implements HandlerInterceptor{@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("拦截器执行");return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {}
}
1. 注解版配置:
创建配置类
com.example.mybatis2018.config.MyMvcConfig
package com.example.mybatis2018.config;import com.example.mybatis2018.interceptor.MyInterCeptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport{@Overrideprotected void addInterceptors(InterceptorRegistry registry) {//注册自己的拦截器,并设置拦截路径,拦截多个可以全一个list集合registry.addInterceptor(new MyInterCeptor()).addPathPatterns("/");}
}
2. 配置文件版 (官方不推荐使用):
定义配置文件
interceptor.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!--配置拦截器-->
<mvc:interceptors><mvc:interceptor><mvc:mapping path="/"/><bean class="com.example.mybatis2018.interceptor.MyInterCeptor"></bean></mvc:interceptor>
</mvc:interceptors></beans>
启动类加载配置文件
com.example.mybatis2018.Mybatis2018Application
package com.example.mybatis2018;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;@MapperScan("com.example.mybatis2018.mapper")
@ImportResource(locations="classpath: interceptor.xml")//扫描xml文件,注入容器
@SpringBootApplication
public class Mybatis2018Application {public static void main(String[] args) {SpringApplication.run(Mybatis2018Application.class, args);}
}
SpringBoot拦截器配置相关推荐
- springboot 添加拦截器之后中文乱码_spring boot 2.x 添加拦截器配置未生效的问题
背景: 今天有一个需求需要拦截除登录相关请求以外的所有请求,并查看request 中是否包含指定的信息,而自然就想到了使用拦截器就可以轻松实现 编写拦截器,获取请求头信息中的test,并打印出来 @C ...
- springboot + 拦截器 + 注解 实现自定义权限验证
springboot + 拦截器 + 注解 实现自定义权限验证 最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定 ...
- 【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener
=================6.SpringBoot拦截器实战和 Servlet3.0自定义Filter.Listener ============ 1.深入SpringBoot2.x过滤器Fi ...
- SpringBoot拦截器与过滤器
一.拦截器与过滤器 在讲Spring boot之前,我们先了解一下过滤器和拦截器.这两者在功能方面很类似,但是在具体技术实现方面,差距还是比较大的.在分析两者的区别之前,我们先理解一下AOP的概念,A ...
- springboot 拦截器_Spring Boot入门系列(十)如何使用拦截器,一学就会!
前面介绍了Spring Boot 如何整合定时任务已经Spring Boot 如何创建异步任务,不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhon ...
- springboot文件上传和下载工具_SpringBoot图文教程7—SpringBoot拦截器的使用姿势这都有...
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...
- Spring拦截器配置详解(如何定义一个拦截器)
一.前言 Spring和SpringBoot的拦截器也是对请求进行的系列验证或处理,关于拦截器和过滤器的区别此文不做介绍,之前我看到过一篇相关系列的文章,讲述的还比较详细,给大家参考参考:拦截器与过滤 ...
- SpringBoot 拦截器和AOP自定义注解进行数据拦截实例
声明:本文纯属个人随手笔记,如果对您有参考价值我十分开心,如果有存在错误,或者有更好的解决办法也麻烦您留言告诉我,大家共同成长,切勿恶言相. 欢迎加入资源共享QQ群:275343679,一起发现知识. ...
- SpringBoot拦截器
SpringBoot拦截器 新建一个拦截器CommonInterceptor,继承HandlerInterceptorAdapter.给大家说一下,在继承HandlerInterceptorAdapt ...
最新文章
- 大学计算机基础课程报告python-Python程序设计习题解析(大学计算机基础教育规划教材)...
- buildroot的使用简介【转】
- 逆向so_记一次APP的so层算法逆向(七)
- 如何在苹果官网下载旧版本的Xcode 方法
- nginx 启动失败
- deeping安装Android studio
- 友盟推送:测试模式推送完成,但却收不到推送信息
- 编写C\Cpp程序中函数声明定义中的常见错误
- Unity导出exe时遇到的两个问题
- 10月各国最新签证及入境政策汇总
- visual studio code远程连接服务器
- for循环的学习心得
- cad转dxf格式文件太大,怎样操作将多张CAD图纸文件转换成高版本的DXF格式?
- PHP API 接口文档生成 简单版本 基于一位大哥的代码改的
- EAUML日拱一卒-微信小程序实战:位置闹铃 (12)-这还不算完
- 调用系统相机录像,压缩保存到相册(附仿微信视频录制demo)
- java双人对战五子棋(socket通信)
- 锤子如何连接MacOS系统调试
- 服务端如何推送消息给客户端?
- VNPY官方开源项目为什么不能选择MIT开源协议