spring boot拦截器配置
1.在spring boot配置文件application.properties中添加要拦截的链接
com.url.interceptor=/user/test
2.编写拦截器代码 ,创建UrlInterceptorUtil 类
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Properties; @Component
public class UrlInterceptorUtil extends HandlerInterceptorAdapter { private String noCheckUrls;
private static Properties props ; @Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String url=request.getRequestURI();
Resource resource = new ClassPathResource("/application.properties");//
props = PropertiesLoaderUtils.loadProperties(resource);
noCheckUrls=props.getProperty("com.url.interceptor","");
if(!noCheckUrls.contains(url)){
return true;
}
HttpSession session=request.getSession();
//业务逻辑处理
if (session.getAttribute("User")==null){ response.sendRedirect("/index");
return false;
}
return true;
} @Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object o, ModelAndView mav)
throws Exception {
// System.out.println("postHandle");
} @Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object o, Exception excptn)
throws Exception {
// System.out.println("afterCompletion");
}
}
3.配置spring boot拦截
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//标注此文件为一个配置项,spring boot才会扫描到该配置。该注解类似于之前使用xml进行配置
@Configuration
public class CustomWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//对所有的请求进行拦截
registry.addInterceptor(new UrlInterceptorUtil()).addPathPatterns("/**");
}
}
spring boot拦截器配置的更多相关文章
- 【spring boot】spring boot 拦截器
今日份代码: 1.定义拦截器 import com.alibaba.fastjson.JSON; import org.apache.commons.collections.CollectionUti ...
- Spring boot拦截器的实现
Spring boot拦截器的实现 Spring boot自带HandlerInterceptor,可通过继承它来实现拦截功能,其的功能跟过滤器类似,但是提供更精细的的控制能力. 1.注册拦截器 @C ...
- Spring MVC拦截器配置
Spring MVC拦截器配置 (1)自定义拦截器 package learnspringboot.xiao.other; import org.springframework.web.servlet ...
- spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案
Springboot中静态资源和拦截器处理(踩了坑) 背景: 在项目中我使用了自定义的Filter 这时候过滤了很多路径,当然对静态资源我是直接放过去的,但是,还是出现了静态资源没办法访问到spr ...
- (22)Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】
上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API,与Spring 没什么关系. Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Sprin ...
- spring原拦截器配置与新命名空间mvc:interceptors配置拦截器对照与注意事项
原先,我们是这么配置拦截器的 <bean id="openSessionInViewInterceptor"class="org.springframework.o ...
- Spring Boot拦截器实现并和swagger集成后使用拦截器的配置问题
1. 定义拦截器 LoginInterceptor LoginInterceptor.java是整个登录认证模块中的核心类之一,它实现了HandlerInterceptor类,由它来拦截并过滤到来的每 ...
- 解决Spring Boot 拦截器注入service为空的问题
问题:在自定义拦截器中,使用了@Autowaire注解注入了封装JPA方法的Service,结果发现无法注入,注入的service为空 0.原因分析 拦截器加载的时间点在springcontext之前 ...
- spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案(转)
文章转自 http://blog.51cto.com/12066352/2093750 最近项目采用spring icloud,用的spring boot版本是1.5.x的,spring boot 2 ...
随机推荐
- 1093 字符串A+B (20 分)
给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集.要求先输出 A,再输出 B,但重复的字符必须被剔除. 输入格式: 输入在两行中分别给出 A 和 B,均为长度不超过 106 ...
- JS禁用浏览器退格键实现思路及代码
[From] http://www.jb51.net/article/42562.htm 上周提交了一个项目(内部使用的),一同事提出个BUG,说要禁用退格键(backspace或者叫后退键),因为这 ...
- python 函数的作用域,闭包函数的用法
一.三元表达式 if条件成功的值 if 条件 else else条件成功的值 #if条件成立的结果 if 条件 else else条件成立的结果 # a = 20 # b = 10 # c ...
- Win10小娜关闭或删除进程
先来说下我为什么想尽方法关闭win10小娜:我觉得功能并不适用于我,即便不启用Cortana小娜,在Win10进程中也会看到Cortana小娜启动着,耗费了内存.CPU,而且主要的我的磁盘利用率等都居 ...
- method reference
import java.util.Arrays; import java.util.List; import java.util.function.Function; import java.util ...
- PHP 编译安装 gd 库
作者博文地址:https://www.cnblogs.com/liu-shuai/ 安装gd依赖库 freetype wget http://download.savannah.gnu.org/rel ...
- Windows x64位通过PEB获得Kernel32基地址
在64位系统下 gs:[0x30] 指向TEB gs:[0x60] 指向PEB kd> dt _TEB nt!_TEB +0x000 NtTib : _NT_TIB +0x000 Excepti ...
- jQuery中的动画——《锋利的JQuery》
自CSS3以来,主流网站开始偏向于扁平风格和动画效果,这时就可以jQuery的动画就可以发挥其长处了,灵活的应用其动画API,让我们可以设计出很多绚丽的效果.下面,让我们来列举一些jQuery常用的动 ...
- 利用pandas生成csv文件
# -*- coding:UTF-8 -*- import json from collections import OrderedDict with open('dns_status.json',' ...
- css三角块
html: <div class="angle"></div> css: .angle{ width: 0px; height: 0px; border-b ...