咋个:在云南方言是怎么的意思,标题的意思就是“拦截器怎么使用?”太多的概念我们在这里就不细讲,主要讲一讲怎么使用,给大家讲一个舍友Mic的故事:Mic是我的一个大学舍友,刚上大学的时候 相中了隔壁班的一个女生,在我们的怂恿下,他终于要到了那个女生的号码,趁着周末Mic约这个女生去看某个明星的演唱会(假设没有大麦等等,需要现场购票),过安检的时候,检票人员发现Mic和那个女生没有买票,就阻止他们进入会场并告知他们到什么地方买票,他们来到了售票大厅买了票顺利的通过检票进入会场,其他舍友和我刚好遇到会场,让Mic出来拿一个充电宝,Mic出来之后又一次通过的检票区进入了会场,和女神度过了难忘的一天。(以上纯属瞎编乱造)故事逻辑如上图其中检票去扮演的是拦截器的角色,售票大厅扮演Cookie登记角色,会场扮演请求地址直接贴代码:登陆界面<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%><!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title>Login</title> </head> <!--style--> <link href="${pageContext.request.contextPath}/css/materialize.min.css" rel="stylesheet" type="text/css" /> <link href="${pageContext.request.contextPath}/css/materialize-icon.css" rel="stylesheet" type="text/css" /> <link href="${pageContext.request.contextPath}/css/common/common.css" rel="stylesheet" type="text/css" /> <link href="${pageContext.request.contextPath}/css/font-awesome.min.css" rel="stylesheet" type="text/css" /> <!--script--> <script src="${pageContext.request.contextPath}/js/jquery-2.1.0.js"></script> <body> <!--内容开始--> <div class="row"> <div class="col l4 s12 m12 offset-l4"> <div class="row"> <div class="col l12 m12 s12 center-align"> <img src="img/logo.png" width="150"/> </div> </div> <div class="row"> <div class="input-field col l12 m12 s12"> <input type="email" id="email" class="validate"/> <label data-error="username format error" data-success="username format success">username</label> </div> </div> <div class="row"> <div class="input-field col l12 m12 s12"> <input type="password" id="password" class="validate" minlength="6"/> <label data-error="password count is minlength 6" data-success="password count is success">password</label> </div> </div> <div class="row"> <div class="col l12 s12 m12"> <a class="btn waves-effect waves-light" id="login_btn" onclick="dologin()">Login</a> </div> </div> </div> </div> <!--内容结束--> </body> <script src="${pageContext.request.contextPath}/js/materialize.min.js"></script> <script src="${pageContext.request.contextPath}/js/admin/admin.js"></script> <script> jQuery(function($) { $(".button-collapse").sideNav(); }) </script></html>效果:登陆成功需要写入cookie:User user = userMapper.selectUserByEmail(loginDto.getEmail()); String md5 = Md5Util.md5(loginDto.getPassword()); if(user==null || StringUtils.isEmpty(user)){ return null; } if(!(user.getPassword()).equals(md5)){ return null; } StringBuilder data = new StringBuilder(); data.append(user.getEmail()).append("|").append(user.getPortraitUrl()); //写入cookie,CookieUtil为自己写的工具类 CookieUtil.setCookie(res, data.toString(), Constant.COOKIE_NAME); TempUser tempUser = new TempUser(); tempUser.setEmail(user.getEmail()); tempUser.setPortraitUrl(user.getPortraitUrl()); return tempUser;拦截器配置用于拦截/admin/下的所有子项目: <!--Springmvc 配置拦截器--> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/admin/**" /> <bean class="com.blog.interceptor.LoginInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>实现HandlerInterceptor接口:判断是否有Cookie存在,若存在放行,若不存在跳转到登录界面package com.blog.interceptor;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.util.StringUtils;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import com.blog.util.Constant;import com.blog.util.CookieUtil;public class LoginInterceptor implements HandlerInterceptor{ @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { } @Override public void postHandle(HttpServletRequest req, HttpServletResponse res, Object arg2, ModelAndView arg3) throws Exception { } @Override public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object arg2) throws Exception { /* * 网站前缀 */ String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+"/"; boolean cookie = CookieUtil.getCookie(req, Constant.COOKIE_NAME); if(cookie==false){ res.sendRedirect(basePath+"login"); return false; } return true; }}若没有登录成功:访问http://localhost:8080/Blog/admin/index跳转回登录界面若登陆成功写入Cookie:访问http://localhost:8080/Blog/admin/index 放行最近正在写自己的个人博客,也想把它当作应聘工作时的项目展示,希望大家多给我提提意见,谢谢!
