欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何在SpringBoot中添加攔截器忽略請(qǐng)求URL當(dāng)中的指定字符串

 更新時(shí)間:2023年08月22日 09:41:04   作者:@小匠  
這篇文章主要介紹了在SpringBoot中添加攔截器忽略請(qǐng)求URL當(dāng)中的指定字符串,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在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方法

    這篇文章主要介紹了為什么要慎重使用ArrayList中的subList方法,subList是List接口中定義的一個(gè)方法,該方法主要用于返回一個(gè)集合中的一段、可以理解為截取一個(gè)集合中的部分元素,他的返回值也是一個(gè)List。,需要的朋友可以參考下
    2019-06-06
  • MultipartFile中transferTo(File file)的路徑問(wèn)題及解決

    MultipartFile中transferTo(File file)的路徑問(wèn)題及解決

    這篇文章主要介紹了MultipartFile中transferTo(File file)的路徑問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java二維數(shù)組指定不同長(zhǎng)度實(shí)例方法

    java二維數(shù)組指定不同長(zhǎng)度實(shí)例方法

    在本篇內(nèi)容里小編給大家分享了一篇關(guān)于java二維數(shù)組指定不同長(zhǎng)度實(shí)例方法,有興趣的朋友們可以學(xué)習(xí)下。
    2021-03-03
  • Spring?Cloud?Ribbon?負(fù)載均衡使用策略示例詳解

    Spring?Cloud?Ribbon?負(fù)載均衡使用策略示例詳解

    Spring?Cloud?Ribbon?是基于Netflix?Ribbon?實(shí)現(xiàn)的一套客戶(hù)端負(fù)載均衡工具,Ribbon客戶(hù)端組件提供了一系列的完善的配置,如超時(shí),重試等,這篇文章主要介紹了Spring?Cloud?Ribbon?負(fù)載均衡使用策略示例詳解,需要的朋友可以參考下
    2023-03-03
  • Spring中SmartLifecycle的用法解讀

    Spring中SmartLifecycle的用法解讀

    這篇文章主要介紹了Spring中SmartLifecycle的用法解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot實(shí)現(xiàn)防止XSS攻擊的示例詳解

    SpringBoot實(shí)現(xiàn)防止XSS攻擊的示例詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot如何實(shí)現(xiàn)防止XSS攻擊,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • 詳解Java中Collection集合的常用方法

    詳解Java中Collection集合的常用方法

    本篇文章給大家?guī)?lái)的內(nèi)容是關(guān)于Java中Collection集合的常用方法詳解,有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你有所幫助。下面我們就來(lái)學(xué)習(xí)一下吧
    2021-11-11
  • 詳細(xì)聊聊Spring MVC重定向與轉(zhuǎn)發(fā)

    詳細(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-09
  • Spring Security實(shí)現(xiàn)身份認(rèn)證和授權(quán)的示例代碼

    Spring 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
  • Java實(shí)現(xiàn)的傅里葉變化算法示例

    Java實(shí)現(xiàn)的傅里葉變化算法示例

    這篇文章主要介紹了Java實(shí)現(xiàn)的傅里葉變化算法,結(jié)合具體實(shí)例形式分析了基于Java的傅里葉變化算法定義與使用相關(guān)操作技巧,需要的朋友可以參考下
    2018-06-06

最新評(píng)論