Spring MVC 拦截器
本文最后更新于:2024年9月8日 晚上
Spring MVC 拦截器
概述
- Spring MVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理,开发者可以自己定义一些拦截器来实现特定的功能。
- 过滤器
- servlet规范中的一部分,任何java web工程都可以使用。
- 在url-pattern中配置了
/*
之后,可以对所有要访问的资源进行拦截。
- 拦截器
- 拦截器是Spring MVC框架自己的,只有使用了Spring MVC框架的工程才能使用。
- 拦截器只会拦截访问的控制器方法,如果访问的是
jsp/html/css/image/js
是不会进行拦截的。
- 过滤器与拦截器的区别:拦截器是AOP思想的具体应用。
自定义拦截器
- 自定义拦截器,必须实现 HandlerInterceptor 接口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class MyInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("------------处理前------------"); return true; }
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponsehttpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("------------处理后------------"); }
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("------------清理------------"); } }
|
配置拦截器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.example.interceptor.MyInterceptor"/> </mvc:interceptor>
<bean name="handlerInterceptor1" class="com.briup.web.interceptor.MyInterceptor1"/> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> <property name="interceptors"> <list> <ref bean="handlerInterceptor1"/> </list> </property> </bean> </mvc:interceptors>
|
SpringBoot
1 2 3 4 5 6 7
| @Configuration public class DemoMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new DemoLoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/user/login","/css/**","/js/**","/img/**"); } }
|
实例
实现思路
- 有一个登陆页面,需要写一个controller访问页面。
- 登陆页面有一提交表单的动作,需要在controller中处理,判断用户名密码是否正确,如果正确,向session中写入用户信息,返回登陆成功。
- 拦截用户请求,判断用户是否登陆,如果用户已经登陆,放行,如果用户未登陆,跳转到登陆页面。
测试:
- 编写一个登陆页面 login.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head>
<h1>登录页面</h1> <hr>
<body> <form action="${pageContext.request.contextPath}/user/login"> 用户名:<input type="text" name="username"> <br> 密码:<input type="password" name="pwd"> <br> <input type="submit" value="提交"> </form> </body> </html>
|
- 编写一个登陆成功的页面 success.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body>
<h1>登录成功页面</h1> <hr>
${user} <a href="${pageContext.request.contextPath}/user/logout">注销</a> </body> </html>
|
- 编写一个Controller处理请求。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| @Controller @RequestMapping("/user") public class UserController {
@RequestMapping("/jumplogin") public String jumpLogin() throws Exception { return "login"; }
@RequestMapping("/jumpSuccess") public String jumpSuccess() throws Exception { return "success"; }
@RequestMapping("/login") public String login(HttpSession session, String username, String pwd) throwsException { System.out.println("接收前端==="+username); session.setAttribute("user", username); return "success"; }
@RequestMapping("logout") public String logout(HttpSession session) throws Exception { session.invalidate(); return "login"; } }
|
- 编写用户登录拦截器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public class LoginInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException { System.out.println("url: " + request.getRequestURI()); if (request.getRequestURI().contains("login")) { return true; }
HttpSession session = request.getSession();
if(session.getAttribute("user") != null) { return true; }
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response); return false; }
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponsehttpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
} }
|
- 在Spring MVC的配置文件中注册拦截器。
1 2 3 4 5 6 7
| <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean id="loginInterceptor" class="com.example.interceptor.LoginInterceptor"/> </mvc:interceptor> </mvc:interceptors>
|