7.1 简介
所谓的拦截器,就相当于servlet里面的过滤器,是AOP思想的应用。此次内容则是,SpringMVC拦截器,自会拦截访问控制器的方法,不会拦截js、html、css、image、jsp。
7.2 用户登录拦截
index.jsp:
<%--
Created by IntelliJ IDEA.
User: yun
Date: 2021/10/11
Time: 15:44
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>
<h1>
<a href="${pageContext.request.contextPath}/user/gologin">登录页面</a>
</h1>
<h1>
<a href="${pageContext.request.contextPath}/user/main">首页</a>
</h1>
</body>
</html>
login.jsp:
<%--
Created by IntelliJ IDEA.
User: yun
Date: 2021/10/11
Time: 16:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<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" value="登录">
</form>
</body>
</html>
home.jsp:
<%--
Created by IntelliJ IDEA.
User: yun
Date: 2021/10/11
Time: 16:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<h1>这是首页</h1>
<p>
用户名:${user}
</p>
<p>密码:${pwd}</p>
<p>
<a href="${pageContext.request.contextPath}/user/logout">注销</a>
</p>
</body>
</html>
LoginController.java:
package com.yun.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/user")
public class LoginController {
@RequestMapping("/main")
public String goMain(){
return "home";
}
@RequestMapping("/gologin")
public String goLogin(){
return "login";
}
@RequestMapping("/login")
public String login(HttpSession session, String name, String pwd, Model model){
session.setAttribute("userLoginInfo",name);
model.addAttribute("user",name);
model.addAttribute("pwd",pwd);
return "home";
}
@RequestMapping("/logout")
public String logout(HttpSession session){
session.removeAttribute("userLoginInfo");
return "./index.jsp";
}
}
MyInterceptor.java:
package com.yun.config;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class MyInterceptor implements HandlerInterceptor {
//是否执行下一个拦截器
//执行顺序是:preHandle,controller,postHandle,afterCompletion
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截前");
HttpSession session = request.getSession();
String flag = (String) session.getAttribute("userLoginInfo");
if (request.getRequestURI().contains("gologin")||request.getRequestURI().contains("login"))
return true;
if (flag!=null)
return true;
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
return false;
}
//主要是为了添加日志
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
//这也是日志添加地
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
application.xml:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.yun.controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<!-- 这是拦截器配置-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/**"/>
<bean class="com.yun.config.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
以上步骤,都是为了对于用户操作进行拦截,例如没有登录就不准进入首页。