所有功能完成 配置登录认证

配置拦截器

在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 设置拦截器的更多相关文章

  1. Spring Boot2(七):拦截器和过滤器

    一.前言 过滤器和拦截器两者都具有AOP的切面思想,关于aop切面,可以看上一篇文章.过滤器filter和拦截器interceptor都属于面向切面编程的具体实现. 二.过滤器 过滤器工作原理 从上图 ...

  2. Spring boot2.0 设置文件上传大小限制

    今天把Spring boot版本升级到了2.0后,发现原来的文件上传大小限制设置不起作用了,原来的application.properties设置如下: spring.http.multipart.m ...

  3. Spring Boot2.0 静态资源被拦截问题

    在Spring Boot2.0+的版本中,只要用户自定义了拦截器,则静态资源会被拦截.但是在spring1.0+的版本中,是不会拦截静态资源的. 因此,在使用Spring Boot2.0+时,配置拦截 ...

  4. Spring AOP原理及拦截器

    原理 AOP(Aspect Oriented Programming),也就是面向方面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP将应用系统分为两部分,核心业务逻辑(Core bu ...

  5. Spring Boot2.0+中,自定义配置类扩展springMVC的功能

    在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js.css等). @Configur ...

  6. Spring Boot2.0 整合 Kafka

    Kafka 概述 Apache Kafka 是一个分布式流处理平台,用于构建实时的数据管道和流式的应用.它可以让你发布和订阅流式的记录,可以储存流式的记录,并且有较好的容错性,可以在流式记录产生时就进 ...

  7. spring boot 实现mybatis拦截器

    spring boot 实现mybatis拦截器 项目是个报表系统,服务端是简单的Java web架构,直接在请求参数里面加了个query id参数,就是mybatis mapper的query id ...

  8. 【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- ...

  9. 【spring colud】spring cloud微服务项目搭建【spring boot2.0】

    spring cloud微服务项目搭建 =================================== 示例版本: 1.spring boot 2.0版本 2.开发工具 IntellJ IDE ...

随机推荐

  1. Analysis of Web.xml in Hello1 project

    一.web.xml文件介绍 The web.xml file contains several elements that are required for a Facelets applicatio ...

  2. Adams 2013自定义插件方法zz

    1.Adams插件介绍 Adams的高级模块(如Controls控制模块.Vibration振动模块.Durability耐久性模块等)是以插件的形式集成在Adams软件中.通过Adams提供的插件管 ...

  3. <转载>Vim的寄存器(复制黏贴要用)

    https://blog.csdn.net/hk2291976/article/details/42196559 消除高亮 :noh

  4. CGI、FastCGI、PHP-FPM联系与区别(理解总结自其他博文)

    参考:http://blog.csdn.net/tyrantbear/article/details/52077321 参考:http://mp.weixin.qq.com/s?src=11& ...

  5. centOS7.3新安装后,设置IP,以及Putty远程和Xshell远程 (学习是个持续的过程,也许中途放松过,但是仍然能重新捡起来,并学以致用,方为真勇士)

    有一段时间没有写学习心得了:现在开始加油,再接再励. 从最基础的开始 1.安装centOS7.3之后设置IP地址.一般linux的系统都是作为服务器的系统来使用,服务器的属性注定了他的IP不能随意的更 ...

  6. h5、css3基础

    一.html(超文本标记语言) 作用:实现页面布局 页面由许多标记符号组成 由浏览器解释执行 二.html主题创建方式 !(英文状态)+tab html:4s+tab html:5+tab 三.标签 ...

  7. 论python中的函数参数的传递问题。

    python是完全面向对象的语言,在参数传递的过程不能使用值传递,引用传递的概念,而应该使用immutable和mutable.在java中,除了object,其实还有8种基本数据类型,才有了参数传递 ...

  8. Nginx服务器 配置 https

    参考 这里 1. 购买证书 2. 补全信息 3. 下载证书( .pem + .key ) 4. 上传至服务器  /usr/local/nginx/conf/cert 下 5. 修改 nginx.con ...

  9. MVCAPi Httpclient

    APi配制文件 删除修改api 显示和命名空间 新增

  10. HTTP Status 500 - Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    今天整合ssm框架 时 遇到的问题 困扰我好长时间     原因就是  mapper文件 没有被加载进来 但是 为什么没有被加载进来呢  因为中间的配置文件出了一些问题 网上大多数说法是   在pom ...