zoukankan      html  css  js  c++  java
  • springboot之路径拦截器

    方式一:不推荐,在代码中添加路径

    1、写一个拦截器,继承HandlerInterceptor类

    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @Component
    public class ConfigPathInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            System.out.println("执行了拦截器");
            if(request.getMethod().equalsIgnoreCase("GET")){
                return true;
            }
    
            return false;
        }
    }

    2、将拦截器添加至拦截器配置中

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import java.util.ArrayList;
    import java.util.List;
    
    @Configuration
    public class InterceptorConfig implements WebMvcConfigurer {
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            List<String> includePathLists= new ArrayList<>();
            includePathLists.add("/showUser");
            List<String> excludePathLists= new ArrayList<>();
            excludePathLists.add("/mybatisPlus");
    //        registry.addInterceptor(authenticationInterceptor())
    //                .addPathPatterns("/**");    // 拦截所有请求,通过判断是否有 @LoginRequired 注解 决定是否需要登录
            registry.addInterceptor(configPathInterceptor())
                    .excludePathPatterns(excludePathLists)   //  不拦截
                    .addPathPatterns(includePathLists); //  拦截
        }
        @Bean
        public AuthenticationInterceptor authenticationInterceptor() {
            return new AuthenticationInterceptor();
        }
    
        @Bean
        public ConfigPathInterceptor configPathInterceptor() {
            return new ConfigPathInterceptor();
        }
    }

    这样就完成了拦截器

    方式二:推荐,在配置文件中添加路径

    1、在application.yml文件中添加路径

    #默认使用配置
    spring:
      profiles:
        active: @activatedProperties@
      redis:
        host: 127.0.0.1
        port: 6379
        password:
        lettuce:
          pool:
            max-active: 100
            max-idle: 10
            max-wait: 100000
    
    #公共配置与profiles选择无关
    mybatis-plus:
      typeAliasesPackage: com.cn.commodity.entity
      mapperLocations: classpath:mapper/*.xml
    
    logging:
      level:
        com.cn.commodity.dao : debug
    
    config:
      path:
        #该路径下GET请求放行,其他拦截
        normal:
    
        #该路径下任何类型请求均拦截
        special:
          - /showUser
        #该路径下任何请求均放行
        exclude:
          - /mybatisPlus

    2、创建拦截器ConfigPathInterceptor继承HandlerInterceptor

    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @Component
    public class ConfigPathInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            System.out.println("执行了拦截器");
            if(request.getMethod().equalsIgnoreCase("GET")){
                return true;
            }
            return false;
        }
    }

    3、将拦截器添加到配置中

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import java.util.ArrayList;
    import java.util.List;
    
    @Configuration
    @Profile({"prod","test"})  //只有测试和生产环境,拦截器才启作用
    @ConfigurationProperties(prefix = "config.path")  //读取配置文件
    public class InterceptorConfig implements WebMvcConfigurer {
    
        private List<String> normal = new ArrayList<>();
    
        private List<String> special = new ArrayList<>();
    
        private List<String> exclude = new ArrayList<>();
    
        public void setExclude(List<String> exclude) {
            this.exclude = exclude;
        }
    
        public void setNormal(List<String> normal) {
            this.normal = normal;
        }
    
        public void setSpecial(List<String> special) {
            this.special = special;
        }
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
    //        registry.addInterceptor(authenticationInterceptor())
    //                .addPathPatterns("/**");    // 拦截所有请求,通过判断是否有 @LoginRequired 注解 决定是否需要登录
            registry.addInterceptor(configPathInterceptor())
                    .excludePathPatterns(exclude)   //  不拦截
                    .addPathPatterns(special); //  拦截
    
        }
        @Bean
        public AuthenticationInterceptor authenticationInterceptor() {
            return new AuthenticationInterceptor();
        }
    
        @Bean
        public ConfigPathInterceptor configPathInterceptor() {
            return new ConfigPathInterceptor();
        }
    }
  • 相关阅读:
    java基础(一)
    html脚本总结
    python编码规范以及推导式的编写
    性能测试
    IOS 单例分析
    IOS 从一个应用跳转另一个应用
    ios开发 如何在应用内获取当前周围wifi列表和强度 并实现在应用内控制wifi开关
    iOS 获取手机的型号,系统版本,软件名称,软件版本
    ios下最简单的正则,RegexKitLite
    网络编程总结(解析数据,下载文件,确认网络环境)
  • 原文地址:https://www.cnblogs.com/ywjfx/p/11328171.html
Copyright © 2011-2022 走看看