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();
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM