概述
SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。开发者可以自定义拦截器来实现特定的功能。
过滤器和拦截器的区别:
拦截器是AOP思想的具体应用。
过滤器:
拦截器:
自定义拦截器
如何实现拦截器呢?
想要自定义拦截器,必须实现HandlerInterceptor接口。
新建一个Module,springmvc-070Interceptor,添加web支持
配置web.xml和springmvc-servlet.xml文件
新建controller类的测试方法
@GetMapping("/t1")
public String test(){
System.out.println("test()执行。。。");
return "ok";
}
编写一个拦截器:实现HandlerInterceptor接口
public class MyInterceptor implements HandlerInterceptor {
//return 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("=====清理=====");
}
}
在Sptringmvc的配置文件中配置拦截器
<!--拦截器配置-->
<mvc:interceptors>
<mvc:interceptor>
<!--包括这个请求下的所有请求-->
<mvc:mapping path="/**"/>
<bean class="com.kuang.config.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
登陆验证案例
先写一个首页index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1>
<a href="${pageContext.request.contextPath}/user/goLogin">登陆页面</a>
</h1>
<h1>
<a href="${pageContext.request.contextPath}/user/main">首页</a>
</h1>
</body>
</html>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--在WEB-INF下的所有页面或资源,只能通过controller或者servlet进行访问--%>
<h1>登录页面</h1>
<form action="${pageContext.request.contextPath}/user/login" method="post">
用户名:<input type="text" name="username">
密码:<input type="text" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
需要登陆才能进入的首页main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<span>${username}</span>
<p>
<a href="${pageContext.request.contextPath}/user/goOut">注销</a>
</p>
</body>
</html>
登陆拦截器 LoginInterceptor.java
public class LoginInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
if(request.getRequestURI().contains("goLogin"))
return true;
if(request.getRequestURI().contains("login"))
return true;
//放行判断
if(session.getAttribute("userLoginInfo")!=null){
return true;
}
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
//判断什么情况下没有登陆
return false;
}
}
applicationContext.xml中注册拦截器
<mvc:interceptor>
<mvc:mapping path="/user/**"/>
<bean class="com.kuang.config.LoginInterceptor"/>
</mvc:interceptor>
登陆请求 :LoginController.java
@RequestMapping("/goLogin")
public String goLogin(){
return "login";
}
@RequestMapping("/login")
public String login(HttpSession session, String username, String password, Model model){
//正常应该把用户信息存到Session中:
session.setAttribute("userLoginInfo",username);
model.addAttribute("username",username);
return "main";
}
@RequestMapping("/goOut")
public String login(HttpSession session){
// session.invalidate();
session.removeAttribute("userLoginInfo");
return "main";
}
原文:https://www.cnblogs.com/CodeHuba/p/13672371.html