SpringBoot配置攔截器的示例
在SpringBoot中配置攔截器,主要有下面兩個(gè)步驟:
1、繼承接口 HandlerInterceptor,根據(jù)需要重寫其中的三個(gè)類。
2、在配置類中注入該類。
public class MyInterceptor implements HandlerInterceptor {
//controller執(zhí)行之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandler......");
return true;
}
//執(zhí)行完controller執(zhí)行之后、視圖渲染前調(diào)用,可以在該方法里獲取或者修改model
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandler......");
}
//一般用于清理資源
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion......");
}
}
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//1、全部攔截
// registry.addInterceptor(myInterceptor()).addPathPatterns("/**");
//2、攔截指定路徑
registry.addInterceptor(myInterceptor()).addPathPatterns("/hello");
}
@Bean
MyInterceptor myInterceptor(){
return new MyInterceptor();
}
}
寫個(gè)controller測(cè)試一下
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("hello");
return "hello";
}
@RequestMapping("/world")
public String world(){
System.out.println("world");
return "world";
}
}
測(cè)試結(jié)果:
preHandler......
hello
postHandler......
afterCompletion......
world
SpringBoot中還有一終攔截器,WebRequestInterceptor
public class MyWebRequestInterceptor implements WebRequestInterceptor {
@Override
public void preHandle(WebRequest webRequest) throws Exception {
}
@Override
public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {
}
@Override
public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {
}
}
和HandlerInterceptor比較相似,但是可以發(fā)現(xiàn),該攔截器的preHandler返回值為空,說(shuō)明該方法并不影響后面方法的執(zhí)行。那么這個(gè)攔截器存在的目的是什么吶?
點(diǎn)進(jìn)WebRequest:
public interface WebRequest extends RequestAttributes {
@Nullable
String getHeader(String var1);
@Nullable
String[] getHeaderValues(String var1);
Iterator<String> getHeaderNames();
@Nullable
String getParameter(String var1);
@Nullable
String[] getParameterValues(String var1);
Iterator<String> getParameterNames();
Map<String, String[]> getParameterMap();
Locale getLocale();
String getContextPath();
@Nullable
String getRemoteUser();
@Nullable
Principal getUserPrincipal();
boolean isUserInRole(String var1);
boolean isSecure();
發(fā)現(xiàn)對(duì)reques請(qǐng)求中參數(shù)做了進(jìn)一步處理(@Nullable表示可以為空),更加的方便調(diào)用。所以兩個(gè)攔截器的側(cè)重點(diǎn)不同,HandlerInterceptor功能較為強(qiáng)大,可以攔截請(qǐng)求,可以實(shí)現(xiàn)WebRequestInterceptor的所有功能,只是要寫的邏輯代碼要多一點(diǎn)。更而WebRequestInterceptor傾向于簡(jiǎn)化獲取request參數(shù)的過(guò)程以及預(yù)設(shè)參數(shù)供后面的流程使用。
以上就是SpringBoot配置攔截器的示例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot配置攔截器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))
本篇文章主要介紹了詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Maven pom.xml文件中build,plugin標(biāo)簽的使用小結(jié)
本文主要介紹了Maven pom.xml文件中build,plugin標(biāo)簽的使用小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
java實(shí)現(xiàn)微信公眾平臺(tái)發(fā)送模板消息的示例代碼
這篇文章主要介紹了java實(shí)現(xiàn)微信公眾平臺(tái)發(fā)送模板消息的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
SpringMVC使用JsonView針對(duì)統(tǒng)一實(shí)體返回不同信息
這篇文章主要為大家介紹了SpringMVC使用JsonView針對(duì)統(tǒng)一實(shí)體返回不同信息,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
java 結(jié)合jQuery實(shí)現(xiàn)跨域名獲取數(shù)據(jù)的方法
下面小編就為大家?guī)?lái)一篇java 結(jié)合jQuery實(shí)現(xiàn)跨域名獲取數(shù)據(jù)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05
Java使用POI實(shí)現(xiàn)excel文件的導(dǎo)入和導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了Java如何使用POI實(shí)現(xiàn)excel文件的導(dǎo)入和導(dǎo)出功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12

