這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)利用SpringMVC如何實現(xiàn)一個登錄驗證攔截器功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)服務項目包括古冶網(wǎng)站建設、古冶網(wǎng)站制作、古冶網(wǎng)頁制作以及古冶網(wǎng)絡營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,古冶網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到古冶省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!
本例實現(xiàn)登陸時的驗證攔截,采用SpringMVC攔截器來實現(xiàn)
當用戶點擊到網(wǎng)站主頁時要進行攔截,用戶登錄了才能進入網(wǎng)站主頁,否則進入登陸頁面
核心代碼
首先是index.jsp,顯示鏈接
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>首頁</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" > --> </head> <body> <div align="center"> <p><a href="loginpage.html" rel="external nofollow" >登陸</a></p> <p><a href="user/home.html" rel="external nofollow" >用戶中心</a></p> <p><a href="exception.html" rel="external nofollow" >觸發(fā)異常</a></p> </div> </body> </html>
controller類
package com.jikexueyuan.demo.springmvc.lesson4.controller; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import com.jikexueyuan.demo.springmvc.lesson4.constant.Global; import com.jikexueyuan.demo.springmvc.lesson4.exception.MyException; import com.jikexueyuan.demo.springmvc.lesson4.model.User; import com.jikexueyuan.demo.springmvc.lesson4.service.LoginService; /** * 這個例子講解了如何定義MVC三層注解,使用@Resource進行注入,以及使用@RequestMapping、@RequestParam 、@SessionAttributes */ @Controller public class LoginController extends BaseController { @Resource LoginService service; @Resource HttpServletRequest request; @RequestMapping("/exception") public void exception() throws MyException{ throw new MyException("測試springmvc中的異常捕獲"); } @RequestMapping("/loginpage") public String toLoginPage(){ return "/WEB-INF/jsp/login.jsp"; } @RequestMapping("/user/home") public String toUserHome(){ return "/WEB-INF/jsp/userhome.jsp"; } @RequestMapping("/logout") public String logout(){ request.getSession().removeAttribute(Global.USER_SESSION_KEY); return "redirect:/"; } @RequestMapping(value = "/doLogin", method = RequestMethod.POST) public String doLogin(@RequestParam String userName, @RequestParam String password){ try { User user = service.doLogin(userName, password); request.getSession().setAttribute(Global.USER_SESSION_KEY, user); return "redirect:/user/home.html"; } catch (Exception e) { return "/WEB-INF/jsp/login.jsp"; } } }
當點擊用戶中心時,觸發(fā)攔截,相關(guān)配置如下
在spring-mvc.xml中加上攔截配置,攔截所有URL中包含/user/的請求,當然請求用戶中心時就會觸發(fā)這個攔截器了
<mvc:interceptors> <mvc:interceptor> <!-- 攔截所有URL中包含/user/的請求 --> <mvc:mapping path="/user/**"/> <bean class="com.jikexueyuan.demo.springmvc.lesson4.interceptor.LoginInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>
然后是bean指向的具體的interceptor類,如果session保存的用戶信息為null,則跳到login頁面,postHandle和afterCompletion方法都不執(zhí)行,反之都執(zhí)行。
package com.jikexueyuan.demo.springmvc.lesson4.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import com.jikexueyuan.demo.springmvc.lesson4.constant.Global; public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Object user = request.getSession().getAttribute(Global.USER_SESSION_KEY); if (user == null) { System.out.println("尚未登錄,調(diào)到登錄頁面"); response.sendRedirect("/loginpage.html"); return false; } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion"); } }
上述就是小編為大家分享的利用SpringMVC如何實現(xiàn)一個登錄驗證攔截器功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁名稱:利用SpringMVC如何實現(xiàn)一個登錄驗證攔截器功能
文章網(wǎng)址:http://aaarwkj.com/article38/gpepsp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、微信小程序、網(wǎng)站改版、網(wǎng)站策劃、云服務器、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)