• SpringBoot实现登录拦截器


    在springBoot只给你如果不设置登录拦截器的话,在任何情况下都可以访问该项目中的所有资源

    1. 拦截器主要方法两个
      在这里插入图片描述
      1.LoginInterceptor 主要继承HandlerInterceptor 类来实现
    package com.example.erp_project.config;
    
    import com.example.erp_project.entity.UserEntity;
    import jakarta.servlet.http.HttpServletRequest;
    import jakarta.servlet.http.HttpServletResponse;
    import jakarta.servlet.http.HttpSession;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    
    /**
     * @author Lolo don‘t feel
     */
    @Component
    public class LoginInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            HttpSession session = request.getSession();
            //从seesion中取出当前登录的信息,
            Object account = session.getAttribute("phone");
            //如果为空跳转到登录页面,并弹出提示信息
            if (account == null) {
                //System.out.println("用户未登陆");
                //PageController中会跳转到同名的页面
                response.sendRedirect("login");
                return false;
            } else {
                return true;
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    2.WebConfigurer 主要继承WebMvcConfigurer 类实现

    package com.example.erp_project.config;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    /**
     * @author Lolo don‘t feel
     */
    @Configuration
    public class WebConfigurer implements WebMvcConfigurer {
        @Autowired
        private LoginInterceptor loginInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            //拦截所有的资源addPathPatterns("/**"),释放登陆相关的资源excludePathPatterns(
            registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns(
                    "/js/**", "/layui/**", "/dist/sliderVerify/**","/lib/**","/css/**",
                    "/images/**", "/register", "/login", "/restPassword",
                    "/agreement","/auth/login","/auth/register","/auth/restPassword"
            );
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    Linux内核之堆溢出的利用
    Shell 结构化命令
    一篇搞定MyBatisPlus!
    Prophet在R语言中进行时间序列数据预测
    Docker(12)CIG容器重量级监控系统
    11.15 知识总结(模板层、模型层)
    微信小程序实现同一页面左右滑动无限切换上/下一页结合后端请求数据,带滑动动画
    预编译为什么能防止SQL注入?一看你就明白了。预编译原理详解
    【C语言】实践:贪吃蛇小游戏(附源码)
    固定资产管理子系统报表分为什么大类,包括哪些科目
  • 原文地址:https://blog.csdn.net/m0_46590717/article/details/138072150