您好,登錄后才能下訂單哦!
一、自定義攔截器實(shí)現(xiàn) HandlerInterceptor 接口
/** * * Created by zhh on 2018/04/20. */ public class MyInterceptor implements HandlerInterceptor { @Autowired private NetworkProxyInfoService networkProxyInfoService; @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { networkProxyInfoService.getAllNetworkProxyInfoByIsValid(GobalConstant.ProxyValid.VALID); } @Override public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { // TODO Auto-generated method stub return true; } }
二、自定義攔截器配置
/** * * Created by zhh on 2018/04/20. */ @Configuration public class MyWebMvcConfig extends WebMvcConfigurerAdapter { /** * 將自定義攔截器作為Bean寫入配置 * @return */ @Bean public MyInterceptor myInterceptor() { return new MyInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) { /** * 多個攔截器組成一個攔截器鏈 * addPathPatterns 用于添加攔截規(guī)則 * excludePathPatterns 用戶排除攔截 */ registry.addInterceptor(myInterceptor()).addPathPatterns("/**"); super.addInterceptors(registry); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。