zoukankan      html  css  js  c++  java
  • 回顾SpringMVC使用拦截器步骤

    5.1 回顾SpringMVC使用拦截器步骤

    • 自定义拦截器类,实现HandlerInterceptor接口
    • 注册拦截器类

    5.2 Spring Boot使用拦截器步骤

    5.2.1        按照Spring MVC的方式编写一个拦截器类,实现HandlerInterceptor接口

    在03-springboot-web中创建interceptor包,并创建一个LoginInterceptor拦截器

    代码示例:

    复制代码
    package com.bjpowernode.springboot.interceptor;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class LoginInterceptor implements HandlerInterceptor {
        @Override //进入Controller之前执行该方法
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            //登录拦截的业务逻辑
            System.out.println("-------登录请求拦截器--------------");
            System.out.println(request.getRequestURI().toString());
            
            Object object = request.getSession().getAttribute("user");
            if (object == null) {
                System.out.println("用户没有登录");
                return false;
            }
    
            //继续提交请求,false 请求不提交了
            return true;
    
        }
    }
    复制代码

    5.2.2        通过配置类注册拦截器

    在03-springboot-web中创建一个config包,创建一个配置类InterceptorConfig,并实现WebMvcConfigurer接口, 覆盖接口中的addInterceptors方法,并为该配置类添加@Configuration注解,标注此类为一个配置类,让Spring Boot 扫描到,这里的操作就相当于SpringMVC的注册拦截器 ,@Configuration就相当于一个applicationContext-mvc.xml

    代码示例:

    复制代码
    package com.bjpowernode.springboot.config;
    
    import com.bjpowernode.springboot.interceptor.LoginInterceptor;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class InterceptorConfig implements WebMvcConfigurer {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            //需要拦截的路径,/**表示需要拦截所有请求
            String[]addPathPatterns={"/**"};
            //不需要拦截的路径
            String [] excludePathPaterns={
                    "/boot/get",
                    "/boot/post",
                    "/boot/put",
                    "/myservlet"
            };
            //注册一个登录拦截器
            registry.addInterceptor(new LoginInterceptor())
                    .addPathPatterns(addPathPatterns)
                    .excludePathPatterns(excludePathPaterns);
            //注册一个权限拦截器  如果有多个拦截器 ,只需要添加以下一行代码
            //registry.addInterceptor(new LoginInterceptor())
            // .addPathPatterns(addPathPatterns)
            // .excludePathPatterns(excludePathPatterns);
    
        }
    }
    复制代码

    5.2.3        浏览器访问测试是否拦截成功

    访问http://localhost:8080/boot/get 控制台不会输出信息

    访问http://localhost:8080/boot/stu 控制台输出信息  

    转:https://www.cnblogs.com/Tpf386/p/11041319.html

  • 相关阅读:
    XAF-内置初始化数据 (XPO)
    XAF中多对多关系 (XPO)
    Implement Custom Business Classes and Reference Properties 实现自定义业务类和引用属性(XPO)
    Add a Class from the Business Class Library 从业务类库添加类 (XPO)
    Set a One-to-Many Relationship设置一对多关系 (XPO)
    Define the Data Model and Set the Initial Data 定义数据模型并设置初始数据
    Create an XAF Application 创建一个XAF应用程序
    XAF Architecture XAF架构
    功能区控件样式设置
    开源项目CIIP(企业信息管理系统框架).2018.1.0910版更新介绍-上周工作总结
  • 原文地址:https://www.cnblogs.com/smallfa/p/13609161.html
Copyright © 2011-2022 走看看