您好,登錄后才能下訂單哦!
后臺管理頁面往往需要登錄才可以進行操作,這時就需要Seession來記錄登錄狀態
要實現起來也是非常簡單,只需要自定義一個HandlerInterceptor就行了
自定義的HandlerInterceptor也只有短短幾行代碼
public class LoginInterceptor implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception err) throws Exception { } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView mav) throws Exception { } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception { //獲取session里的登錄狀態值 String str = (String) request.getSession().getAttribute("isLogin"); //如果登錄狀態不為空則返回true,返回true則會執行相應controller的方法 if(str!=null){ return true; } //如果登錄狀態為空則重定向到登錄頁面,并返回false,不執行原來controller的方法 response.sendRedirect("/backend/loginPage"); return false; } }
Controller代碼
@Controller @RequestMapping("/backend") public class BackendController { @RequestMapping(value = "/loginPage", method = {RequestMethod.GET}) public String loginPage(HttpServletRequest request,String account, String password){ return "login"; } @RequestMapping(value = "/login", method = {RequestMethod.POST}) public String login(HttpServletRequest request,RedirectAttributes model, String account, String password){ //驗證賬號密碼,如果符合則改變session里的狀態,并重定向到主頁 if ("jack".equals(account)&&"jack2017".equals(password)){ request.getSession().setAttribute("isLogin","yes"); return "redirect:IndexPage"; }else { //密碼錯誤則重定向回登錄頁,并返回錯誤,因為是重定向所要要用到RedirectAttributes model.addFlashAttribute("error","密碼錯誤"); return "redirect:loginPage"; } } //登出,移除登錄狀態并重定向的登錄頁 @RequestMapping(value = "/loginOut", method = {RequestMethod.GET}) public String loginOut(HttpServletRequest request) { request.getSession().removeAttribute("isLogin"); return "redirect:loginPage"; } @RequestMapping(value = "/IndexPage", method = {RequestMethod.GET}) public String IndexPage(HttpServletRequest request){ return "Index"; } }
spring的配置
<!--省略其他基本配置--> <!-- 配置攔截器 --> <mvc:interceptors> <!-- 配置登陸攔截器 --> <mvc:interceptor> <!--攔截后臺頁面的請求--> <mvc:mapping path="/backend/**"/> <!--不攔截登錄頁和登錄的請求--> <mvc:exclude-mapping path="/backend/loginPage"/> <mvc:exclude-mapping path="/backend/login"/> <bean class="com.ima.Interceptor.LoginInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>
一個簡單的Session實現登錄認證系統就這樣完成了,如果想登錄狀態退出瀏覽器后仍保留一段時間的可以將Session改為Cookie
一般情況下我們都會使用Cookie
Cookie和Session的方法差不多
使用Cookie的自定義HandlerInterceptor
public class LoginInterceptor implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception err) throws Exception { } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView mav) throws Exception { } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception { // 獲取request的cookie Cookie[] cookies = request.getCookies(); if (null==cookies) { System.out.println("沒有cookie=============="); } else { // 遍歷cookie如果找到登錄狀態則返回true執行原來controller的方法 for(Cookie cookie : cookies){ if(cookie.getName().equals("isLogin")){ return true; } } } // 沒有找到登錄狀態則重定向到登錄頁,返回false,不執行原來controller的方法 response.sendRedirect("/backend/loginPage"); return false; } }
Controller的變化也不大
@Controller @RequestMapping("/backend") public class BackendController { @RequestMapping(value = "/loginPage", method = {RequestMethod.GET}) public String loginPage(HttpServletRequest request, String account, String password) { return "login"; } @RequestMapping(value = "/login", method = {RequestMethod.POST}) public String login(HttpServletRequest request, HttpServletResponse response, RedirectAttributes model, String account, String password) { if ("edehou".equals(account) && "aidou2017".equals(password)) { Cookie cookie = new Cookie("isLogin", "yes"); cookie.setMaxAge(30 * 60);// 設置為30min cookie.setPath("/"); response.addCookie(cookie); return "redirect:IndexPage"; } else { model.addFlashAttribute("error", "密碼錯誤"); return "redirect:loginPage"; } } @RequestMapping(value = "/logOut", method = {RequestMethod.GET}) public String loginOut(HttpServletRequest request, HttpServletResponse response) { Cookie[] cookies = request.getCookies(); for (Cookie cookie : cookies) { if (cookie.getName().equals("isLogin")) { cookie.setValue(null); cookie.setMaxAge(0);// 立即銷毀cookie cookie.setPath("/"); response.addCookie(cookie); break; } } return "redirect:loginPage"; } @RequestMapping(value = "/IndexPage", method = {RequestMethod.GET}) public String IndexPage(HttpServletRequest request) { return "Index"; } }
spring的配置和之前的一模一樣
注意
這里只是演示,建議在實際項目中Cookie的鍵和值要經過特殊處理,否則會引發安全問題
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。