日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring boot 2.x 拦截器

發布時間:2023/12/18 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot 2.x 拦截器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、spring1.x配置方式

? ? 在spring boot1.x中,使用攔截器,一般進行如下配置:

@Configuration public class AppConfig extends WebMvcConfigurerAdapter {@Resourceprivate FRInterceptor fRInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {//自定義攔截器,添加攔截路徑和排除攔截路徑 registry.addInterceptor(fRInterceptor).addPathPatterns("api/**").excludePathPatterns("api/login");} }

? ??

????但是在spring boot2.x中,WebMvcConfigurerAdapter被deprecated,雖然繼承WebMvcConfigurerAdapter這個類雖然有此便利,但在Spring5.0里面已經deprecated了。

???? 官方文檔也說了,WebMvcConfigurer接口現在已經有了默認的空白方法,所以在Springboot2.0(Spring5.0)下更好的做法還是implements?WebMvcConfigurer

2、spring2.x配置方式

2.1攔截器統一管理

import javax.annotation.Resource;import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import com.spring.pro.interceptor.FileUploadInterceptor;/*** @ClassName: WebConfig* @Description:* @author weiyb* @date 2018年5月7日 上午11:30:58*/ @Configuration public class WebConfig implements WebMvcConfigurer {@Resourceprivate FileUploadInterceptor fileUploadInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 自定義攔截器,添加攔截路徑和排除攔截路徑registry.addInterceptor(fileUploadInterceptor).addPathPatterns("/**");} }

2.2自定義攔截器

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView;/*** 文件上傳攔截器* @ClassName: FileUploadInterceptor* @Description:* @author weiyb* @date 2018年5月7日 上午11:51:53*/ @Component public class FileUploadInterceptor implements HandlerInterceptor {/** 視圖渲染之后的操作*/@Overridepublic void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)throws Exception {}/** 處理請求完成后視圖渲染之前的處理操作*/@Overridepublic void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)throws Exception {}/** 進入controller層之前攔截請求*/@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {System.out.println("getContextPath:" + request.getContextPath());System.out.println("getServletPath:" + request.getServletPath());System.out.println("getRequestURI:" + request.getRequestURI());System.out.println("getRequestURL:" + request.getRequestURL());System.out.println("getRealPath:" + request.getSession().getServletContext().getRealPath("image"));return true;}}

總結

以上是生活随笔為你收集整理的spring boot 2.x 拦截器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。