Spring Boot2.0 设置拦截器
所有功能完成 配置登录认证
配置拦截器
在spring boot2.0 之后 通过继承这个WebMvcConfigurer类 就可以完成拦截
- 新建包com.example.interceptor;
- 创建login拦截类
package com.example.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //请求进入这个拦截器
HttpSession session = request.getSession();
if(session.getAttribute("user") == null){ //判断session中有没有user信息
// System.out.println("进入拦截器");
if("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))){
response.sendError(401);
}
response.sendRedirect("/"); //没有user信息的话进行路由重定向
return false;
}
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 {
}
}
- 在com.example包中添加拦截控制器
package com.example;
import com.example.interceptor.LoginInterceptor;
import com.example.interceptor.RightsInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
@Configuration //使用注解 实现拦截
public class WebAppConfigurer implements WebMvcConfigurer {
@Autowired
RightsInterceptor rightsInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//登录拦截的管理器
InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor()); //拦截的对象会进入这个类中进行判断
registration.addPathPatterns("/**"); //所有路径都被拦截
registration.excludePathPatterns("/","/login","/error","/static/**","/logout"); //添加不拦截路径
}
}
- 在WebAppConfigurer.java中增加内容
package com.example;
import com.example.interceptor.LoginInterceptor;
import com.example.interceptor.RightsInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
@Configuration //使用注解 实现拦截
public class WebAppConfigurer implements WebMvcConfigurer {
@Autowired
RightsInterceptor rightsInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//登录拦截的管理器
InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor()); //拦截的对象会进入这个类中进行判断
registration.addPathPatterns("/**"); //所有路径都被拦截
registration.excludePathPatterns("/","/login","/error","/static/**","/logout"); //添加不拦截路径
// super.addInterceptors(registry);
//权限拦截的管理器
InterceptorRegistration registration1 = registry.addInterceptor(rightsInterceptor);
registration1.addPathPatterns("/**"); //所有路径都被拦截
registration1.excludePathPatterns("/","/login","/error","/static/**","/logout"); //添加不拦截路径
}
}
Spring Boot2.0 设置拦截器的更多相关文章
- Spring Boot2(七):拦截器和过滤器
一.前言 过滤器和拦截器两者都具有AOP的切面思想,关于aop切面,可以看上一篇文章.过滤器filter和拦截器interceptor都属于面向切面编程的具体实现. 二.过滤器 过滤器工作原理 从上图 ...
- Spring boot2.0 设置文件上传大小限制
今天把Spring boot版本升级到了2.0后,发现原来的文件上传大小限制设置不起作用了,原来的application.properties设置如下: spring.http.multipart.m ...
- Spring Boot2.0 静态资源被拦截问题
在Spring Boot2.0+的版本中,只要用户自定义了拦截器,则静态资源会被拦截.但是在spring1.0+的版本中,是不会拦截静态资源的. 因此,在使用Spring Boot2.0+时,配置拦截 ...
- Spring AOP原理及拦截器
原理 AOP(Aspect Oriented Programming),也就是面向方面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP将应用系统分为两部分,核心业务逻辑(Core bu ...
- Spring Boot2.0+中,自定义配置类扩展springMVC的功能
在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js.css等). @Configur ...
- Spring Boot2.0 整合 Kafka
Kafka 概述 Apache Kafka 是一个分布式流处理平台,用于构建实时的数据管道和流式的应用.它可以让你发布和订阅流式的记录,可以储存流式的记录,并且有较好的容错性,可以在流式记录产生时就进 ...
- spring boot 实现mybatis拦截器
spring boot 实现mybatis拦截器 项目是个报表系统,服务端是简单的Java web架构,直接在请求参数里面加了个query id参数,就是mybatis mapper的query id ...
- 【spring cloud】spring cloud2.X spring boot2.0.4调用feign配置Hystrix Dashboard 和 集成Turbine 【解决:Hystrix仪表盘Unable to connect to Command Metric Stream】【解决:Hystrix仪表盘Loading...】
环境: <java.version>1.8</java.version><spring-boot.version>2.0.4.RELEASE</spring- ...
- 【spring colud】spring cloud微服务项目搭建【spring boot2.0】
spring cloud微服务项目搭建 =================================== 示例版本: 1.spring boot 2.0版本 2.开发工具 IntellJ IDE ...
随机推荐
- react使用过程中常见问题
目录 一.减小输入字符数 二.用props.children来引用位于前置标签和后置标签之间的内容 三.创建组件两条主要的途径 四.JSX属性采用驼峰式的大小写规则(即‘onClick’而非‘oncl ...
- 在NSMutableArray中添加空元素:NSNull类的使用
有时需要将一些表示“空”的对象添加到array中.NSNull类正是基于这样的目的产生的.用NSNull表示一个占位符时,语句表达如下: [array addObject:[NSNull null]] ...
- v-charts修改点击图例事件,legendselectchanged
html: <!--折线图--><ve-line :extend="item.chartExtend" :data-zoom="dataZoom&quo ...
- 【安全性测试】drozer中关于AttackSurface的一些理解
在推荐扫描Android APP的工具中,扫描组件可以推荐drozer.使用过drozer的使用者知道,如何查找各个组件上的攻击层面 run app.package.AttackSurface . 它 ...
- margin与padding的bug
1.在页面布局时,值对于块元素来说,相邻的两个兄弟块元素间的margin-top与上一个兄弟的margin-bottom重合时, 解决办法:对其中一个块元素中设置 display:inline- ...
- iview修改tabbar实现小程序自定义中间圆形导航栏及多页面登录功能
emmm,用iview改了个自定义中间圆形的tabbar. 如下图所示, 重点,什么鬼是“多页面登录”? 例如:我现在要做一个功能,要说自己长得帅才能进去页面. 一个两个页面还好,但是我现在要每个页面 ...
- [LeetCode] Hand of Straights 一手顺子牌
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into ...
- ES6 常用语法
1.let 定义变量 1.与var 类似 用于声明一个变量 let userName='kobe' 2.特点 1.在块作用域内有效 2.不会吃重复定义变量 3.应用 1.循环遍历加监听 2.使用let ...
- css常用命名
常用的CSS命名 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体佈局宽度:wrapper 左右中 ...
- UEditor之实现配置简单的图片上传示例
UEditor之实现配置简单的图片上传示例 原创 2016年06月11日 18:27:31 开心一笑 下班后,阿华到楼下小超市买毛巾,刚买完出来,就遇到同一办公楼里另一家公司的阿菲,之前与她远远的有过 ...