Springboot項(xiàng)目使用攔截器方法詳解
1. 創(chuàng)建一個(gè)攔截器并實(shí)現(xiàn)HandlerInterceptor接口
package com.leiyuan.bs.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 MyHandlerInterceptor implements HandlerInterceptor { /** * 攔截(Controller方法調(diào)用之前) * * @param request request * @param response response * @param o o * @return 通過與否 * @throws Exception 異常處理 */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception { // TODO 我這里是通過用戶是否登陸進(jìn)行攔截,我的用戶信息存儲(chǔ)在session中,名稱為userSession,大家可以自行實(shí)現(xiàn) if (request.getSession().getAttribute("userSession") == null) { // 攔截至登陸頁面 request.getRequestDispatcher("/user/toLogin").forward(request, response); // false為不通過 return false; } // true為通過 return true; } // 此方法為處理請(qǐng)求之后調(diào)用(調(diào)用過controller方法之后,跳轉(zhuǎn)視圖之前) @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } // 此方法為整個(gè)請(qǐng)求結(jié)束之后進(jìn)行調(diào)用 @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { } }
2. 創(chuàng)建一個(gè)配置類MyHandlerInterceptorConfig并繼承WebMvcConfigurerAdapter類重寫addInterceptors(InterceptorRegistry registry)方法
package com.leiyuan.bs; import com.leiyuan.bs.interceptor.MyHandlerInterceptor; import org.springframework.stereotype.Component; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; // 攔截器配置類 @Component public class MyHandlerInterceptorConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { /** * 這里的addPathPatterns("/**")為配置需要攔截的方法“/**”代表所有,而后excludePathPatterns("/user/toLogin")等方法為排除哪些方法不進(jìn)行 攔截 */ registry.addInterceptor(new MyHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/toLogin").excludePathPatterns ("/user/login").excludePathPatterns("/user/toNewUser").excludePathPatterns("/user/newUser"); super.addInterceptors(registry); } }
3. 啟動(dòng)項(xiàng)目即可看到效果了
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java Web學(xué)習(xí)之MySQL在項(xiàng)目中的使用方法
mysql數(shù)據(jù)庫是我們?cè)谌粘i_發(fā)中經(jīng)常會(huì)用到的,下面這篇文章主要給大家介紹了關(guān)于Java Web學(xué)習(xí)之MySQL在項(xiàng)目中的使用方法,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04mybatis使用foreach查詢不出結(jié)果也不報(bào)錯(cuò)的問題
這篇文章主要介紹了mybatis使用foreach查詢不出結(jié)果也不報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java并發(fā)系列之CountDownLatch源碼分析
這篇文章主要為大家詳細(xì)介紹了Java并發(fā)系列之CountDownLatch源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03mybatis返回map結(jié)果集@MapKey使用的場(chǎng)景分析
這篇文章主要介紹了mybatis返回map結(jié)果集@MapKey使用的場(chǎng)景分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01MyBatis-Plus 如何單元測(cè)試的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis-Plus 如何單元測(cè)試的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08springboot整合Quartz實(shí)現(xiàn)動(dòng)態(tài)配置定時(shí)任務(wù)的方法
本篇文章主要介紹了springboot整合Quartz實(shí)現(xiàn)動(dòng)態(tài)配置定時(shí)任務(wù)的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10詳解SpringBoot啟動(dòng)項(xiàng)目后執(zhí)行方法的幾種方式
在項(xiàng)目開發(fā)中某些場(chǎng)景必須要用到啟動(dòng)項(xiàng)目后立即執(zhí)行方式的功能,本文主要聊聊實(shí)現(xiàn)立即執(zhí)行的幾種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09