简介
类似于Servlet开发中的过滤器Filter,是AOP思想的具体应用。
只会拦截访问的控制器方法, 如果访问的是jsp/html/css/image/js是不会进行拦截的
自定义拦截器
配置对应的web.xml,spring配置文件
1.继承HandlerInterceptor
HandlerInterceptor默认实现这三个方法,可以不覆盖
public class MyInterceptor implements HandlerInterceptor {
//false 不允许进行下一个拦截器,卡死在当前方法
//true 允许进行下一个拦截器
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("-------------拦截前-----------");
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("-------------拦截后-----------");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("-------------清理-----------");
}
}
配置spring配置文件
<mvc:interceptors>
<mvc:interceptor>
<!--/** 代表当前目录包括其子目录下的所有文件都被拦截
/* 只拦截当前目录下的 如/a/* 只拦截/a/b 不会拦截/a/b/c-->
<mvc:mapping path="/**"/>
<!--指明我们所使用的拦截器-->
<bean class="config.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
Controller
@Controller
public class MyController {
@RequestMapping("/t")
@ResponseBody
public String test(){
System.out.println("test is start");
return "test is ok ";
}
}
执行结果
-------------拦截前-----------
test is start
-------------拦截后-----------
-------------清理-----------
实践-登录拦截
自定义拦截器
package config;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LogInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//如果登录界面进行登录,放行
if (request.getRequestURI().contains("login")){
System.out.println("contains---login");
return true;
}
//如果已经登录,放行
if (request.getSession().getAttribute("name")!=null&&!"".equals(request.getSession().getAttribute("name")))
{
System.out.println("name:---------"+request.getSession().getAttribute("name"));
return true;
}
//否则回到登录界面
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
return false;
}
}
配置spring
<mvc:interceptor>
<mvc:mapping path="/user/**"/>
<bean class="config.LogInterceptor"/>
</mvc:interceptor>
前端界面
初始界面
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2021/3/21
Time: 15:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/user/gologin">登录</a>
<a href="${pageContext.request.contextPath}/user/main">首页</a>
</body>
</html>
登录界面
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2021/3/21
Time: 16:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/login" method="post">
<input type="text" name="name">
<input type="password" name="pwd">
<input type="submit">
</form>
</body>
</html>
首页
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2021/3/21
Time: 16:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
首页
${name}
<a href="${pageContext.request.contextPath}/user/goOut">注销</a>
</body>
</html>
Controller
package controller;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/user")
public class MyController {
@RequestMapping("/main")
public String main(){
return "main";
}
@RequestMapping("/gologin")
public String gologin(){
return "login";
}
@RequestMapping("/login")
public String login(String name, String pwd, HttpSession httpSession){
httpSession.setAttribute("name",name);
httpSession.setAttribute("pwd",pwd);
return "main";
}
@RequestMapping("/goOut")
public String goOut(HttpSession httpSession){
httpSession.removeAttribute("name");
httpSession.removeAttribute("pwd");
httpSession.invalidate();
return "login";
}
}