Spring MVC--攔截器實(shí)現(xiàn)和用戶登陸例子
1.攔截器
SpringMvc中的攔截器實(shí)現(xiàn)了HandlerInterceptor接口,通常使用與身份認(rèn)證,授權(quán)和校驗(yàn),模板視圖,統(tǒng)一處理等;
public class HanderInterceptor1 implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
return true;
}
}
在攔截器中有三個(gè)方法 :
preHandler :在進(jìn)入Handler方法之前執(zhí)行了,使用于身份認(rèn)證,身份授權(quán),登陸校驗(yàn)等,比如身份認(rèn)證,用戶沒(méi)有登陸,攔截不再向下執(zhí)行,返回值為 false ,即可實(shí)現(xiàn)攔截;否則,返回true時(shí),攔截不進(jìn)行執(zhí)行;
postHandler : 進(jìn)入Handler方法之后,返回ModelAndView之前執(zhí)行,使用場(chǎng)景從ModelAndView參數(shù)出發(fā),比如,將公用的模型數(shù)據(jù)在這里傳入到視圖,也可以統(tǒng)一指定顯示的視圖等;
afterHandler : 在執(zhí)行Handler完成后執(zhí)行此方法,使用于統(tǒng)一的異常處理,統(tǒng)一的日志處理等;
2.攔截器的配置
攔截器的配置有兩種方式實(shí)現(xiàn) :
(1)針對(duì)某個(gè)handlermapping (controller)的 配置
Springmvc攔截器針對(duì)某個(gè)controller進(jìn)行攔截設(shè)置,如果在某個(gè)HandlerMapping中配置攔截,經(jīng)過(guò)該HandlerMapping映射成功的handler最終使用該攔截器;
(2)類似全局的配置
可以配置類似全局的攔截器,springmvc框架將配置的類似全局?jǐn)r截器注入到每個(gè)Handlermapping中;
配置實(shí)現(xiàn) :
<!-- 配置攔截器 --> <mvc:interceptors> <!-- 多個(gè)攔截器,順序執(zhí)行 --> <mvc:interceptor> <!-- /** 表示所有的url,包括子url路徑 --> <mvc:mapping path="/**"/> <bean class="cn.labelnet.ssm.filter.HanderInterceptor1"></bean> </mvc:interceptor> <!-- 配置登陸攔截器 --> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="cn.labelnet.ssm.filter.LoginHandlerIntercepter"></bean> </mvc:interceptor> <!-- ..... --> </mvc:interceptors>
(3)在一個(gè)工程中,可以配置多個(gè)攔截器,使用多個(gè)攔截器,則要注意的是 :
多個(gè)攔截器使用的時(shí)候,preHandler是順序執(zhí)行的,而postHandler和afterHandler是倒序執(zhí)行的;
所以 :
如果統(tǒng)一日志處理器攔截器,需要改攔截器prehandler一定要返回true,且將它放在攔截器配置的第一個(gè)位置;
如果登陸認(rèn)證攔截器,放在攔截器的配置中的第一個(gè)位置(有日志處理的話,放在日志處理下面);
如果有權(quán)限校驗(yàn)攔截器,則放在登陸攔截器之后,因?yàn)榈顷懲ㄟ^(guò)后,才可以進(jìn)行校驗(yàn)權(quán)限;
3.示例:
場(chǎng)景描述 :用戶點(diǎn)擊查看的時(shí)候,我們進(jìn)行登陸攔截器操作,判斷用戶是否登陸? 登陸,則不攔截,沒(méi)登陸,則轉(zhuǎn)到登陸界面;
圖示 :

3.1 controller 登陸業(yè)務(wù)實(shí)現(xiàn)
@RequestMapping("/clientLogin")
public String clientLogin(HttpSession httpSession,String username,String password){
if(username.equals("yuan")&&password.equals("123456")){
//登陸成功
httpSession.setAttribute("username",username);
return "forward:clientsList.action";
}else{
//登陸失敗
return "forward:login.jsp";
}
}
3.2 controller 登出業(yè)務(wù)實(shí)現(xiàn)
@RequestMapping("/clientLoginOut")
public String clientLoginOut(HttpSession httpSession){
httpSession.invalidate();
return "forward:clientsList.action";
}
3.3 攔截器實(shí)現(xiàn)
在這里實(shí)現(xiàn)用戶攔截實(shí)現(xiàn)是:通過(guò)判斷是否是編輯查看的頁(yè)面,如果是,判斷session中的用戶名存在不存在,就可以了;
package cn.labelnet.ssm.filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
* 登陸攔截器
* 場(chǎng)景:用戶點(diǎn)擊查看的時(shí)候,我們進(jìn)行登陸攔截器操作,判斷用戶是否登陸?
* 登陸,則不攔截,沒(méi)登陸,則轉(zhuǎn)到登陸界面;
* TODO
* 作者:原明卓
* 時(shí)間:2016年1月8日 下午3:25:35
* 工程:SpringMvcMybatis1Demo
*/
public class LoginHandlerIntercepter implements HandlerInterceptor {
@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 {
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse arg1,
Object arg2) throws Exception {
String requestURI = request.getRequestURI();
if(requestURI.indexOf("editClientIfo.action")>0){
//說(shuō)明處在編輯的頁(yè)面
HttpSession session = request.getSession();
String username = (String) session.getAttribute("username");
if(username!=null){
//登陸成功的用戶
return true;
}else{
//沒(méi)有登陸,轉(zhuǎn)向登陸界面
request.getRequestDispatcher("/login.jsp").forward(request,arg1);
return false;
}
}else{
return true;
}
}
}
3.4 攔截器配置實(shí)現(xiàn)
<!-- 配置攔截器 --> <mvc:interceptors> <!-- 多個(gè)攔截器,順序執(zhí)行 --> <mvc:interceptor> <!-- /** 表示所有的url,包括子url路徑 --> <mvc:mapping path="/**"/> <bean class="cn.labelnet.ssm.filter.HanderInterceptor1"></bean> </mvc:interceptor> <!-- 配置登陸攔截器 --> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="cn.labelnet.ssm.filter.LoginHandlerIntercepter"></bean> </mvc:interceptor> <!-- ..... --> </mvc:interceptors>
3.5 登陸頁(yè)面實(shí)現(xiàn)
<%@ 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>
<form action="${pageContext.request.contextPath }/clients/clientLogin.action" method="post">
姓名:<input type="text" name="username"> <br><br>
密碼: <input type="text" name="password"> <br><br>
<input type="submit" value="登陸">
</form>
</body>
</html>
3.6 列表信息頁(yè)面
<body>
<h1>客戶信息管理 <br>
<c:if test="${username!=null }">
${username}
<a href="${pageContext.request.contextPath}/clients/clientLoginOut.action" rel="external nofollow" >退出</a>
</c:if>
</h1>
<form method="post" action="" style="margin-top: 10px;float: left;margin-left: 5%;">
<input id="search" type="text" >
<input value="查詢" type="submit">
</form>
<table width="90%" border="1" align="center">
<thead>
<tr>
<td colspan="10" align="center"> 客戶信息管理</td>
</tr>
</thead>
<tbody>
<tr align="center">
<td>編號(hào)</td>
<td>姓名</td>
<td>代碼</td>
<td>生日</td>
<td>家庭住址</td>
<td>現(xiàn)居住地</td>
<td>聯(lián)系方式</td>
<td>緊急聯(lián)系方式</td>
<td>注冊(cè)日期</td>
<td>操作</td>
</tr>
<c:forEach items="${clients}" var="c">
<tr>
<td> ${c.id} </td>
<td> ${c.username} </td>
<td> ${c.client_certificate_no} </td>
<td> ${c.born_date} </td>
<td> ${c.family_register_address} </td>
<td> ${c.now_address} </td>
<td> ${c.contact_mode} </td>
<td> ${c.urgency_contact_mode} </td>
<td> ${c.create_date} </td>
<td><a href="${pageContext.request.contextPath}/clients/editClientIfo.action?id=${c.id}" rel="external nofollow" >查看</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- SpringCloud實(shí)現(xiàn)Redis在各個(gè)微服務(wù)的Session共享問(wèn)題
- SpringBoot跨系統(tǒng)單點(diǎn)登陸的實(shí)現(xiàn)方法
- springboot登陸頁(yè)面圖片驗(yàn)證碼簡(jiǎn)單的web項(xiàng)目實(shí)現(xiàn)
- spring boot整合CAS Client實(shí)現(xiàn)單點(diǎn)登陸驗(yàn)證的示例
- Spring boot搭建web應(yīng)用集成thymeleaf模板實(shí)現(xiàn)登陸
- Spring security實(shí)現(xiàn)登陸和權(quán)限角色控制
- springcloud微服務(wù)基于redis集群的單點(diǎn)登錄實(shí)現(xiàn)解析
相關(guān)文章
SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源的切換實(shí)踐
這篇主要介紹了SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源的切換,本文基于AOP來(lái)實(shí)現(xiàn)數(shù)據(jù)源的切換,文中通過(guò)示例代碼介紹的非常詳細(xì),感興趣的小伙伴們可以參考一下2022-03-03
SpringBoot配置Redis實(shí)現(xiàn)保存獲取和刪除數(shù)據(jù)
本文主要介紹了SpringBoot配置Redis實(shí)現(xiàn)保存獲取和刪除數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
在springboot中注入FilterRegistrationBean不生效的原因
這篇文章主要介紹了在springboot中注入FilterRegistrationBean不生效的原因及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java之?dāng)?shù)組在指定位置插入元素實(shí)現(xiàn)
本文主要介紹了Java之?dāng)?shù)組在指定位置插入元素實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
雙token實(shí)現(xiàn)token超時(shí)策略示例
用于restful的app應(yīng)用無(wú)狀態(tài)無(wú)sesion登錄示例,需要的朋友可以參考下2014-02-02
在Java中使用redisTemplate操作緩存的方法示例
這篇文章主要介紹了在Java中使用redisTemplate操作緩存的方法示例,在Redis中可以存儲(chǔ)String、List、Set、Hash、Zset。感興趣的可以了解一下2019-01-01
SpringBoot項(xiàng)目啟動(dòng)時(shí)預(yù)加載操作方法
Spring Boot是一種流行的Java開發(fā)框架,它提供了許多方便的功能來(lái)簡(jiǎn)化應(yīng)用程序的開發(fā)和部署,這篇文章主要介紹了SpringBoot項(xiàng)目啟動(dòng)時(shí)預(yù)加載,需要的朋友可以參考下2023-09-09
mybatis-plus?分頁(yè)類型轉(zhuǎn)換工具類
用mybatis-plus?的分頁(yè)對(duì)象的時(shí)候,因?yàn)橛胢ybatis-puls?查詢出來(lái)的分頁(yè)對(duì)象的records里的泛型是實(shí)體,有時(shí)候需要將實(shí)體轉(zhuǎn)換為前端展示的對(duì)象,所有寫了一個(gè)分頁(yè)數(shù)據(jù)的類型轉(zhuǎn)換工具,解決這個(gè)問(wèn)題,對(duì)mybatis-plus?分頁(yè)工具類相關(guān)知識(shí)感興趣的朋友一起看看吧2022-03-03

