如何在SpringBoot中添加攔截器忽略請(qǐng)求URL當(dāng)中的指定字符串
在SpringBoot中添加攔截器忽略請(qǐng)求URL當(dāng)中的指定字符串
1 自定義攔截器
@Component public class GlobalInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String path = request.getRequestURI(); if (path.contains("/api/")) { path = path.replaceAll("/api/", "/"); request.getRequestDispatcher(path).forward(request, response); } return true; } }
2 注冊(cè)攔截器
@Configuration @EnableWebMvc public class WebMvcConfig implements WebMvcConfigurer { GlobalInterceptor globalInterceptor; @Autowired public WebMvcConfig(GlobalInterceptor globalInterceptor) { this.globalInterceptor = globalInterceptor; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(globalInterceptor).addPathPatterns("/api/**"); } }
SpringBoot 使用攔截器(配置特定的url請(qǐng)求會(huì)進(jìn)入攔截器)
SpringBoot 使用攔截器步驟為:
1、按照Spring mvc的方式編寫(xiě)一個(gè)攔截器類(lèi);
創(chuàng)建一個(gè)interceptor包
LoginInterceptor: package com.springboot.web.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginInterceptor implements HandlerInterceptor { //主要是這個(gè)方法中實(shí)現(xiàn)攔截邏輯 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("已經(jīng)進(jìn)入攔截器"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
2、編寫(xiě)一個(gè)配置類(lèi)繼承WebMvcConfigurerAdapter類(lèi)
3、為該配置類(lèi)添加@Configuration注解,標(biāo)注此類(lèi)為一個(gè)配置類(lèi),讓Spring boot 掃描到;
4、覆蓋其中的addInterceptors方法并添加已經(jīng)編寫(xiě)好的攔截器:
代碼如下:編寫(xiě)WebConfig配置類(lèi)
package com.springboot.web.config; import com.springboot.web.interceptor.LoginInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration //一定要加上這個(gè)注解,成為Springboot的配置類(lèi),不然不會(huì)生效 public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { String[] addPathPatterns = {//攔截所有請(qǐng)求 "/*" }; String[] excludePatterns = {//不攔截sayHi這個(gè)請(qǐng)求 【http://localhost:8080/sayHi】 "/sayHi" }; //如果有多個(gè)攔截器,可以繼續(xù)添加 registry.addInterceptor(new LoginInterceptor()).addPathPatterns(addPathPatterns).excludePathPatterns(excludePatterns); } }
測(cè)試下:
【http://localhost:8080/config】會(huì)打印“已經(jīng)進(jìn)入攔截器”,說(shuō)明攔截器生效
【http://localhost:8080/sayHi】不會(huì)打印,說(shuō)明不攔截配置生效。
到此這篇關(guān)于如何在SpringBoot中添加攔截器忽略請(qǐng)求URL當(dāng)中的指定字符串的文章就介紹到這了,更多相關(guān)SpringBoot添加攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
為什么阿里要慎重使用ArrayList中的subList方法
這篇文章主要介紹了為什么要慎重使用ArrayList中的subList方法,subList是List接口中定義的一個(gè)方法,該方法主要用于返回一個(gè)集合中的一段、可以理解為截取一個(gè)集合中的部分元素,他的返回值也是一個(gè)List。,需要的朋友可以參考下2019-06-06MultipartFile中transferTo(File file)的路徑問(wèn)題及解決
這篇文章主要介紹了MultipartFile中transferTo(File file)的路徑問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07java二維數(shù)組指定不同長(zhǎng)度實(shí)例方法
在本篇內(nèi)容里小編給大家分享了一篇關(guān)于java二維數(shù)組指定不同長(zhǎng)度實(shí)例方法,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03Spring?Cloud?Ribbon?負(fù)載均衡使用策略示例詳解
Spring?Cloud?Ribbon?是基于Netflix?Ribbon?實(shí)現(xiàn)的一套客戶(hù)端負(fù)載均衡工具,Ribbon客戶(hù)端組件提供了一系列的完善的配置,如超時(shí),重試等,這篇文章主要介紹了Spring?Cloud?Ribbon?負(fù)載均衡使用策略示例詳解,需要的朋友可以參考下2023-03-03SpringBoot實(shí)現(xiàn)防止XSS攻擊的示例詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何實(shí)現(xiàn)防止XSS攻擊,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03詳細(xì)聊聊Spring MVC重定向與轉(zhuǎn)發(fā)
大家應(yīng)該都知道請(qǐng)求重定向和請(qǐng)求轉(zhuǎn)發(fā)都是web開(kāi)發(fā)中資源跳轉(zhuǎn)的方式,這篇文章主要給大家介紹了關(guān)于Spring MVC重定向與轉(zhuǎn)發(fā)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09Spring Security實(shí)現(xiàn)身份認(rèn)證和授權(quán)的示例代碼
在 Spring Boot 應(yīng)用中使用 Spring Security 可以非常方便地實(shí)現(xiàn)用戶(hù)身份認(rèn)證和授權(quán),本文主要介紹了Spring Security實(shí)現(xiàn)身份認(rèn)證和授權(quán)的示例代碼,感興趣的可以了解一下2023-06-06