您好,登錄后才能下訂單哦!
HandlerInterceptor如何在springMVC中使用?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
一、HandlerInterceptor用法
第一步:注冊攔截器
<!-- 注冊攔截器 --> <mvc:interceptors> <bean class="com.project.base.interceptor.ControlInterceptor" /> </mvc:interceptors>
第二步:繼承HandlerInterceptor ,實現攔截器
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()); /** * 執行完控制器后調用,即離開時 */ @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; } }
二、后臺打印信息效果
三、拓展使用攔截器實現http基本認證
/** * 執行完控制器后調用,即離開時 */ @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"); }
看完上述內容,你們掌握HandlerInterceptor如何在springMVC中使用的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。