10.1 需求
1、用戶請求url
2、攔截器進行攔截校驗
如果請求的url是公開地址(無需登陸即可訪問的url),讓放行
如果用戶session 不存在跳轉到登陸頁面
如果用戶session存在放行,繼續操作。
10.2 登陸controller方法
@Controller public class LoginController { // 登陸 @RequestMapping("/login") public String login(HttpSession session, String username, String password) throws Exception { // 調用service進行用戶身份驗證 // ... // 在session中保存用戶身份信息 session.setAttribute("username", username); // 重定向到商品列表頁面 return "redirect:/items/queryItems.action"; } // 退出 @RequestMapping("/logout") public String logout(HttpSession session) throws Exception { // 清除session session.invalidate(); // 重定向到商品列表頁面 return "redirect:/items/queryItems.action"; } }
//登陸提交 //userid:用戶賬號,pwd:密碼 @RequestMapping("/login") public String loginsubmit(HttpSession session,String userid,String pwd)throws Exception{ //調用service進行用戶身份認證 //…… //向session記錄用戶身份信息 session.setAttribute("activeUser", userid); return "redirect:item/queryItem.action"; } //退出 @RequestMapping("/logout") public String logout(HttpSession session)throws Exception{ //session過期 session.invalidate(); return "redirect:item/queryItem.action"; }
10.3 登陸認證攔截實現
public class LoginInterceptor implements HandlerInterceptor { //進入 Handler方法之前執行 //用於身份認證、身份授權 //比如身份認證,如果認證通過表示當前用戶沒有登陸,需要此方法攔截不再向下執行 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //獲取請求的url String url = request.getRequestURI(); //判斷url是否是公開 地址(實際使用時將公開 地址配置配置文件中) //這里公開地址是登陸提交的地址 if(url.indexOf("login.action")>=0){ //如果進行登陸提交,放行 return true; } //判斷session HttpSession session = request.getSession(); //從session中取出用戶身份信息 String username = (String) session.getAttribute("username"); if(username != null){ //身份存在,放行 return true; } //執行這里表示用戶身份需要認證,跳轉登陸頁面 request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response); //return false表示攔截,不向下執行 //return true表示放行 return false; }
public class LoginInterceptor implements HandlerInterceptor{ @Override Public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //如果是登錄頁面則放行 if(request.getRequestURI().indexOf("login.action")>=0){ return true; } HttpSession session = request.getSession(); //如果用戶已登錄也放行 if(session.getAttribute("user")!=null){ return true; } //用戶沒有登錄挑戰到登錄頁面 request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response); return false; } }
10.4 攔截器配置 springmvc.xml
<!--攔截器 --> <mvc:interceptors> <!--多個攔截器,順序執行 --> <!-- 登陸認證攔截器 --> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="cn.itcast.ssm.interceptor.LoginInterceptor"></bean> </mvc:interceptor> <mvc:interceptor> <!-- /**表示所有url包括子url路徑 --> <mvc:mapping path="/**"/> <bean class="cn.itcast.ssm.interceptor.HandlerInterceptor1"></bean> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="cn.itcast.ssm.interceptor.HandlerInterceptor2"></bean> </mvc:interceptor> </mvc:interceptors>
10.5 itemsList.jsp
當前用戶:${username }, <c:if test="${username!=null }"> <a href="${pageContext.request.contextPath }/logout.action">退出</a> </c:if>
10.6 login.jsp
<form action="${pageContext.request.contextPath }/login.action" method="post"> 用戶賬號:<input type="text" name="username" /><br/> 用戶密碼 :<input type="password" name="password" /><br/> <input type="submit" value="登陸"/>