springMVC攔截器HandlerInterceptor用法代碼示例
摘要:很多時候我們都會去修改其他同事的bug,甚至是已經(jīng)離職的同事的bug,有時候我們點(diǎn)擊頁面去不著到后臺對應(yīng)的是哪個controller,針對這個問題,其實(shí)我們可以通過sprngmvc的攔截器來攔擊用戶的請求從而知道頁面請求的是哪個class的哪個方法,當(dāng)然這些打印日志信息肯能并不適合放在生產(chǎn)環(huán)境,或者這個攔截器也是非必要的。。。。
一、HandlerInterceptor用法
第一步:注冊攔截器
<!-- 注冊攔截器 --> <mvc:interceptors> <bean class="com.project.base.interceptor.ControlInterceptor" /> </mvc:interceptors>
第二步:繼承HandlerInterceptor ,實(shí)現(xiàn)攔截器
package com.iflashbuy.limanman; import java.util.Iterator; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class ControlInterceptor implements HandlerInterceptor{ private Logger logger = LoggerFactory.getLogger(getClass()); /** * 執(zhí)行完控制器后調(diào)用,即離開時 */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3) throws Exception { } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } @Override public Boolean preHandle(HttpServletRequest request, HttpServletResponse arg1, Object arg2) throws Exception { int i = 1; try { @SuppressWarnings("unchecked") Map<String, Object> parmMap = request.getParameterMap(); Iterator<String> iter = parmMap.keySet().iterator(); while (iter.hasNext()) { Object key = iter.next(); Object value = parmMap.get(key); logger.info("第" + i + "個param---->{}-{}", key, value); i = i + 1; } } catch (Exception e) { i = 1; } i = 1; return true; } }
二、后臺打印信息效果
三、拓展使用攔截器實(shí)現(xiàn)http基本認(rèn)證
/** * 執(zhí)行完控制器后調(diào)用,即離開時 */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3) throws Exception { logger.info("className--->" + arg2); logger.info("request--->" + request); } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3) throws Exception { } @Override public Boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception { String sessionAuth = (String) request.getSession().getAttribute("auth"); if (sessionAuth != null) { System.out.println("this is next step"); nextStep(request, response); } else { if (!checkHeaderAuth(request, response)) { response.setStatus(401); response.setHeader("Cache-Control", "no-store"); response.setDateHeader("Expires", 0); response.setHeader("WWW-authenticate", "Basic Realm=\"請輸入管理員賬號密碼\""); return false; } } return true; } private Boolean checkHeaderAuth(HttpServletRequest request, HttpServletResponse response) throws IOException { String auth = request.getHeader("Authorization"); System.out.println("auth encoded in base64 is " + getFromBASE64(auth)); if ((auth != null) && (auth.length() > 6)) { auth = auth.substring(6, auth.length()); String decodedAuth = getFromBASE64(auth); System.out.println("auth decoded from base64 is " + decodedAuth); request.getSession().setAttribute("auth", decodedAuth); return true; } else { return false; } } private String getFromBASE64(String s) { if (s == null) return null; try { byte[] encodeBase64 = Base64.encodeBase64(s.getBytes("UTF-8")); return new String(encodeBase64); } catch (UnsupportedEncodingException e) { return null; } } public void nextStep(HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("you can go to the controller"); }
總結(jié)
以上就是本文關(guān)于springMVC攔截器HandlerInterceptor用法代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
Spring攔截器HandlerInterceptor接口代碼解析
Spring MVC實(shí)現(xiàn)的登錄攔截器代碼分享
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
springboot內(nèi)置tomcat調(diào)優(yōu)并發(fā)線程數(shù)解析
這篇文章主要介紹了springboot內(nèi)置tomcat調(diào)優(yōu)并發(fā)線程數(shù)解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Spring Cloud Alibaba 之 Nacos教程詳解
Nacos是阿里的一個開源產(chǎn)品,它是針對微服務(wù)架構(gòu)中的服務(wù)發(fā)現(xiàn)、配置管理、服務(wù)治理的綜合性解決方案。這篇文章主要介紹了Spring Cloud Alibaba 之 Nacos的相關(guān)知識,需要的朋友可以參考下2020-11-11mybatis的insert語句插入數(shù)據(jù)時的返回值的實(shí)現(xiàn)
這篇文章主要介紹了mybatis的insert語句插入數(shù)據(jù)時的返回值的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10解析阿里一面CyclicBarrier和CountDownLatch的區(qū)別
這篇文章主要介紹了阿里一面CyclicBarrier和CountDownLatch的區(qū)別是啥,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03