博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot2.0 设置拦截器
阅读量:7006 次
发布时间:2019-06-27

本文共 3488 字,大约阅读时间需要 11 分钟。

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

配置拦截器

在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");       //添加不拦截路径    }}

转载于:https://www.cnblogs.com/rolandlee/p/9580492.html

你可能感兴趣的文章
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
查看>>
DB2 锁升级示例1
查看>>
启动网卡报错Failed to start LSB: Bring up/down network
查看>>
centos 升级4.0 内核
查看>>
Ubuntu配置永久生效的alias总结
查看>>
Tcp/Ip Http Socket的区别
查看>>
理解 Keystone 核心概念 - 每天5分钟玩转 OpenStack(18)
查看>>
我的友情链接
查看>>
tp5, laravel, yii2我该选择哪个
查看>>
compositionEnd 和 input 事件(中文输入法问题)
查看>>
Nginx基本安装配置
查看>>
Nginx支持php配置
查看>>
观察者模式
查看>>
轻量级IOC容器IOCFactory发布。
查看>>
js 关闭当前页面
查看>>
OCP笔记部分整理-学习参考
查看>>
Fedora 中使用逻辑卷管理器调整LVM分区大小
查看>>
IOS TableView Cell重用机制
查看>>
label中for属性
查看>>
VS2010的快捷键;VS2012变化的快捷键
查看>>