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

配置拦截器

在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. mac 启动php-fpm报错 failed to open configuration file '/private/etc/php-fpm.conf': No such file or direc

    直接运行,有报错找不到配置文件. $ php-fpm [11-Jan-2014 16:03:03] ERROR: failed to open configuration file '/private ...

  2. arp断网攻击

    arp断网攻击可以是同局域网内主机无法上网!(可恶搞室友哦,嘻嘻!) 实现原理 arp的中文释义是地址解析协议,全英文 address resolution protocol,是一个将局域网IP地址映 ...

  3. Android应用程序支持不同屏幕(尺寸、密度)

    how to build a user interface using Android layouts for all types of devices 使用Android布局设计的UI接口用于不同的 ...

  4. IntelliJ IDEA部署web项目,Tomcat没有出现Artifacts

    解决办法:

  5. JavaScript模板引擎Handlebars

    Handlebars模板库简单介绍 Handlebars是JavaScript一个语义模板库,通过对view(模板)和data(ajax请求的数据,一般是json)的分离来快速构建Web模板.它采用& ...

  6. 通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明。

    在Spring相应包导入正确的前提下,出现这个异常,是因为我们在引入命名空间的时候,没有正确引入它的DTD解析文件,以上面的context为例,解决办法如下: 在引入 xmlns:context=&q ...

  7. Qt5+MSVC2015编译器编译发布的Release程序运行崩溃,如何查找崩溃的原因??

    除了加log信息,还有什么方法?? ==================================2019/4/26============================= 1 常见的c++内 ...

  8. 开启IIS Express可以调试X64项目

    //来源:https://blog.csdn.net/ma_jiang/article/details/52679431 现在项目开发时总有时需要在X64下开发,这样我们就需要IIS Express中 ...

  9. js计算指定日期的下一年的日期

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. SQL DISTINCT去掉重复的数据统计方法【转】

    SELECT指令让我们能够读取表格中一个或数个栏位的所有资料.这将把所有的资料都抓出,无论资料值有无重复.在资料处理中,我们会经常碰到需要找出表格内的不同资料值的情况.换句话说,我们需要知道这个表格/ ...