您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。