SpringBoot攔截器

(1) 配置需攔截urls

young:
  interceptUrls:
    - /member/**
    - /product/**
@Data
@RefreshScope
@Configuration
@ConfigurationProperties(prefix = "young")
public class IntercepterConfig {
    private List<String> interceptUrls;
}

(2) 編寫攔截器業(yè)務(wù)

@Component
public class UnloginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return false;
    }
}

(3) 添加攔截器

@SpringBootConfiguration
public class YoungWebMvcConfig implements WebMvcConfigurer {

    @Autowired
    IntercepterConfig intercepterConfig;

    @Autowired
    UnloginInterceptor unloginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> intercepterUrls = intercepterConfig.getInterceptUrls();
//        String[] patterns = intercepterUrls.toArray(new String[intercepterUrls.size()]);
        registry.addInterceptor(unloginInterceptor).addPathPatterns(intercepterUrls);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 所有的開發(fā)之中攔截器一定是一個(gè)必須要使用的功能,利用攔截器可以更加有效的實(shí)現(xiàn)數(shù)據(jù)的驗(yàn)證處理,而且最為幸運(yùn)的是在 S...
    左羊閱讀 661評(píng)論 0 1
  • 大家好,我是IT修真院武漢分院第11期學(xué)員,一枚正直善良的java程序員。今天給大家分享一下,任務(wù)拓展:sprin...
    心魔_4cef閱讀 732評(píng)論 0 0
  • JAVA && Spring && SpringBoot2.x — 學(xué)習(xí)目錄 1. 攔截器的基本使用 1.1 攔...
    小胖學(xué)編程閱讀 6,983評(píng)論 0 12
  • 過濾器屬于servlet api范疇,與spring 關(guān)系不大。Web開發(fā)中,我們除了使用 Filter 來過濾請(qǐng)...
    林太浪先生閱讀 242評(píng)論 0 0
  • 攔截器是指通過統(tǒng)一攔截從瀏覽器發(fā)往服務(wù)器的請(qǐng)求來完成功能的增強(qiáng) 應(yīng)用場景:解決一些共性問題,比如權(quán)限驗(yàn)證、亂碼等 ...
    fangzy0723閱讀 895評(píng)論 1 1

友情鏈接更多精彩內(nèi)容