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 ...
随机推荐
- Dear friends:
To realize the value of ONE YEAR想知道一整年的价值ask the student who has failed a class就去问被当过的学生To realize ...
- springboot(二)框架整合
我们做web项目或者写api接口通常使用的是springmvc+spring+mybatis+mysql,那么使用springboot之后,默认是集成了所有的后台框架,只需要添加dependency依 ...
- 数组的indexOf方法--数组去重
数组的indexOf方法 数组方法大家再熟悉不过了,却忽略了数组有 indexOf 这个方法(我个人感觉). 干说不练瞎扯淡,遇到了什么问题,注意⚠️点又在哪里? let arr = ['orange ...
- vue $index,$key已经移除了
之前可以这样: <ul id="example"> <li v-for="item in items"> {{$index}} {{$k ...
- linux命令eval的用法
[转自]http://blog.chinaunix.net/uid-21411227-id-1826706.html 1. eval command-line 其中command-line是在终端上键 ...
- python_面向对象—代码练习
"""注意:代码切勿照搬,错误请留言指出""" import re ''' class Person: name='xxx' age=20 ...
- 5. AQS(AbstractQueuedSynchronizer)抽象的队列式的同步器
5.1 AbstractQueuedSynchronizer里面的设计模式--模板模式 模板模式:父类定义好了算法的框架,第一步做什么第二步做什么,同时把某些步骤的实现延迟到子类去实现. 5.1.1 ...
- Python+Selenium定位元素的方法
Python+Selenium有以下八种定位元素的方法: 1. find_element_by_id() eg: find_element_by_id("kw") 2. find_ ...
- linux 下安装 mysql 并配置 python 开发环境
1.安装 mysql ,安装过程中将提示设置 root 用户的密码,默认可以设置为 rootadmin . $ sudo apt-get install mysql-server 2.安装 mysql ...
- 【Linux相识相知】文件查找(locate/find)
在用linux操作系统的时候,当我们忘记之前某个文件存储的位置,但是知道其文件名或者模糊的知道其文件名,我们都可以通过文件查找工具来查找,linux提供两种常用的查找工具,locate和find,在日 ...