前言
SpringMVC 中的Interceptor 攔截器主要用于攔截用戶的請求并進(jìn)行相應(yīng)的處理质帅,定義一個Interceptor主要有兩種方式:
- 實(shí)現(xiàn)HandlerInterceptor 接口详囤,或者是繼承實(shí)現(xiàn)了HandlerInterceptor 接口的類艘蹋,例如HandlerInterceptorAdapter瞻赶;
-
實(shí)現(xiàn)Spring的WebRequestInterceptor接口,或者是繼承實(shí)現(xiàn)了WebRequestInterceptor的類尘执。
攔截器應(yīng)用場景
以qq郵箱登錄為例躏吊,登錄成功后,會進(jìn)入到個人郵箱頁面稍刀。在短時間內(nèi)撩独,即使關(guān)閉了登錄頁面(不退出),再打開登錄頁账月,也會跳轉(zhuǎn)到個人郵箱頁面综膀。如果退出,再打開個人郵箱頁面鏈接局齿,則會跳轉(zhuǎn)到登錄頁面剧劝。以上登錄狀態(tài)的檢查其實(shí)就是測試過程中常聽說的session檢查了,也就是說每一個 Controller 方法執(zhí)行前(上圖的preHandle)都需要進(jìn)行 session 登錄態(tài)的檢查抓歼,如果存在登錄態(tài)讥此,則繼續(xù)執(zhí)行
Controller 的方法,如果不存在谣妻,則直接返回萄喳。
攔截器示例
參考《開發(fā)測試spring應(yīng)用》書中提到的登錄案例,對攔截器的應(yīng)用做以下說明蹋半。
pom依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springDemo</groupId>
<artifactId>springMVCDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springMVCDemo Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>4.2.8.RELEASE</spring.version>
<!-- 解決mvn編譯亂碼問題
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-->
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!-- springframe start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- springframe end -->
</dependencies>
<build>
<finalName>springMVCDemo</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!--
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
-->
</plugins>
</pluginManagement>
</build>
</project>
新建jsp及controller
login.jsp:登錄頁面
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2019/2/25
Time: 15:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>測試系統(tǒng)</title>
</head>
<body>
<form action="/login" method="get">
<table>
<tr>
<td>用戶名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2">
<button type="submit">登錄</button>
</td>
</tr>
</table>
</form>
</body>
</html>
welcome.jsp:需注意他巨,某些版本的isELIgnored默認(rèn)為true,此時需要設(shè)置為isELIgnored="false",否則${username}變量無法傳遞到頁面展現(xiàn)染突。
<%--
Created by IntelliJ IDEA.
User: lenovo
Date: 2019/2/25
Time: 15:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>測試系統(tǒng)</title>
</head>
<body>
<div>歡迎${username}登錄測試系統(tǒng)澳硪!</div>
<div><a href="/logout">退出</a> </div>
</body>
</html>
LoginController.java:“redirect:+路徑”表示重定向跳轉(zhuǎn)份企。代碼中用到的session也榄,有效時間可以在web.xml中設(shè)置。
<!--session有效時間為30分鐘-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
package example.controller;
import example.info.LoginInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Controller
public class LoginController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String login(Map<String, Object> map) {
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(LoginInfo loginInfo, HttpSession httpSession) { //此處的參數(shù)傳遞使用的是LoginInfo 實(shí)體對象
System.out.println("username:"+loginInfo.getUsername());
if (loginInfo.getUsername() != null){
httpSession.setAttribute("username", loginInfo.getUsername());
return "redirect:/welcome"; //點(diǎn)擊登錄按鈕后薪棒,重定向跳轉(zhuǎn)到 /welcome 路徑
}else
return "redirect:/"; //如果直接訪問/login路徑手蝎,則跳轉(zhuǎn)到登錄頁
}
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String welcome(Map<String, Object> map, HttpSession httpSession) {
String username = "";
if (httpSession.getAttribute("username") != null)
username = httpSession.getAttribute("username").toString();
map.put("username", username); //傳遞變量給welcome.jsp的${username}
return "welcome";
}
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout(HttpSession httpSession) {
httpSession.setAttribute("username", null);
return "redirect:/";
}
}
新建實(shí)體類LoginInfo
需注意的是榕莺,LoginInfo 定義的變量必須與login.jsp標(biāo)簽里的 name 屬性對應(yīng)俐芯,并且要有g(shù)etter/setter 方法花墩,這樣才能在表單提交時自動賦值搞乏。
package example.info;
public class LoginInfo {
private String username;
private String password;
public String getUsername() {
return this.username;
}
public String getPassword() {
return this.password;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
}
回看LoginController類的login方法,除了通過實(shí)體類LoginInfo來傳遞參數(shù)蕴轨,
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(LoginInfo loginInfo, HttpSession httpSession) {
System.out.println("username:"+loginInfo.getUsername());
if (loginInfo.getUsername() != null){
httpSession.setAttribute("username", loginInfo.getUsername());
return "redirect:/welcome"; //重定向跳轉(zhuǎn)到 /welcome 路徑
}else
return "redirect:/"; //如果直接訪問/login路徑唠雕,則跳轉(zhuǎn)到登錄頁
}
還可以通過以下兩種方式來實(shí)現(xiàn)同樣的效果贸营。
方式一:變量名依舊和login.jsp標(biāo)簽的name屬性名一致。
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(String username,String password, HttpSession httpSession) {
if (username != null){
httpSession.setAttribute("username", username);
return "redirect:/welcome"; //重定向跳轉(zhuǎn)到 /welcome 路徑
}else
return "redirect:/"; //如果直接訪問/login路徑岩睁,則跳轉(zhuǎn)到登錄頁
}
方式二:通過@RequestParam注解來實(shí)現(xiàn)钞脂,此方式不要求變量名與login.jsp標(biāo)簽屬性一致。
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(@RequestParam("username")String name, HttpSession httpSession) {
if (name != null){
httpSession.setAttribute("username", name);
return "redirect:/welcome"; //重定向跳轉(zhuǎn)到 /welcome 路徑
}else
return "redirect:/"; //如果直接訪問/login路徑捕儒,則跳轉(zhuǎn)到登錄頁
}
新建攔截類DemoInterceptor
前文提到每一個Controller方法執(zhí)行前都需要進(jìn)行session登錄態(tài)的檢查冰啃,此時便需要用到攔截器了,接下來以實(shí)現(xiàn)HandlerInterceptor 接口為例說明刘莹。
package example.interceptor;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DemoInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
String username = (String) httpServletRequest.getSession().getAttribute("username");
System.out.println("name:"+username);
if (StringUtils.isEmpty(username)) {
httpServletResponse.sendRedirect(httpServletRequest.getContextPath());
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
- preHandle 是攔截前置處理阎毅,在請求進(jìn)入 action 之前執(zhí)行,通過重寫preHandle 方法点弯,判斷了 session 信息是否存在扇调,如果存在,則返回 true抢肛,然后進(jìn)入 action狼钮,如果不存在,則 sendRedirect,捡絮,即進(jìn)行重定向熬芜,request.getContextPath() 的值就是站點(diǎn)根目錄”http://localhost:8082/”,所以會進(jìn)入登錄頁面锦援。
- postHandle 就是攔截后置處理猛蔽。
- afterCompletion 就是攔截完成處理。
配置攔截器
配置dispatcher-servlet.xml,增加攔截器interceptor配置曼库。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="example.controller"/>
<mvc:default-servlet-handler/>
<!--啟用spring的一些annotation -->
<context:annotation-config/>
<!-- 配置注解驅(qū)動 可以將request參數(shù)與綁定到controller參數(shù)上 -->
<mvc:annotation-driven/>
<!--靜態(tài)資源映射-->
<!--本項(xiàng)目把靜態(tài)資源放在了webapp的statics目錄下区岗,資源映射如下-->
<!--statics目錄下所有文件不會被DispatcherServlet攔截,直接訪問毁枯,當(dāng)做靜態(tài)資源交給Servlet處理-->
<mvc:resources mapping="/statics/**" location="/WEB-INF/statics/"/>
<!--mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/"/-->
<!--mvc:resources mapping="/image/**" location="/WEB-INF/statics/image/"/-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/><!--設(shè)置JSP文件的目錄位置-->
<property name="suffix" value=".jsp"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
</bean>
<!--mvc:mapping 表示要攔截的請求路徑慈缔,”/**”表示攔截所有的路徑-->
<!--mvc:exclude-mapping 表示要排除攔截的路徑,即不攔截的路徑-->
<!--bean 表示攔截后要做的事种玛,都寫在了 DemoInterceptor 這個類里-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/"/>
<mvc:exclude-mapping path="/login"/>
<mvc:exclude-mapping path="/view/**"/>
<bean class="example.interceptor.DemoInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
增加攔截器后藐鹤,未登錄前,打開 http://localhost:8082/welcome會跳轉(zhuǎn)到登錄頁面赂韵。如果不增加攔截器娱节,未登錄前打開 http://localhost:8082/welcome 則會跳轉(zhuǎn)到歡迎頁面。
頁面驗(yàn)證
做完以上處理后祭示,一個登錄頁面基本完成了肄满,啟動Tomcat服務(wù)。
輸入http://locathost:8082质涛,展現(xiàn)登錄頁面如下:
點(diǎn)擊登錄按鈕稠歉,進(jìn)入welcome頁面。
點(diǎn)擊退出按鈕汇陆,回到登錄頁面怒炸。
參考資料
《開發(fā)測試的spring應(yīng)用》