preHandle:在業(yè)務(wù)處理器處理請(qǐng)求之前被調(diào)用。預(yù)處理,可以進(jìn)行編碼、安全控制、權(quán)限校驗(yàn)等處理;
postHandle:在業(yè)務(wù)處理器處理請(qǐng)求執(zhí)行完成后,生成視圖之前執(zhí)行。后處理(調(diào)用了Service并返回ModelAndView,但未進(jìn)行頁(yè)面渲染),有機(jī)會(huì)修改ModelAndView (這個(gè)博主就基本不怎么用了);
afterCompletion:在DispatcherServlet完全處理完請(qǐng)求后被調(diào)用,可用于清理資源等。返回處理(已經(jīng)渲染了頁(yè)面);
成都創(chuàng)新互聯(lián)公司主營(yíng)東勝網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都App制作,東勝h5小程序設(shè)計(jì)搭建,東勝網(wǎng)站營(yíng)銷推廣歡迎東勝等地區(qū)企業(yè)咨詢
package com.xiaohang.socialcard.pre.intercepter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 自定義攔截器
*/
public class SignAutheInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
/**
* 通過(guò)HttpServletRequest和HttpServletResponse做相應(yīng)數(shù)據(jù)處理
*/
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 {
}
}
通過(guò)addPathPatterns屬性可配置需要進(jìn)行攔截的請(qǐng)求路徑
通過(guò)excludePathPatterns屬性可配置不進(jìn)行攔截的請(qǐng)求路徑
package com.xiaohang.socialcard.pre.intercepter;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 配置攔截器
*/
@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SignAutheInterceptor()).addPathPatterns("/api/v1/get/getToken");
super.addInterceptors(registry);
}
}
名稱欄目:SpringCloud使用HandlerInterceptor攔截器
當(dāng)前網(wǎng)址:http://www.rwnh.cn/article6/jihdog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、品牌網(wǎng)站制作、做網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)