springMVC-拦截器

1 基本环境搭建

1.1 创建普通maven项目,添加web支持

1.2 配置web.xml

1.3 配置springmvc配置文件 applicationContext.xml

1.4 创建控制层和拦截器层的两个包

2 简单测试拦截器

2.1 在拦截器层创建一个拦截器类

package com.lv.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor {
    //return true; 执行下一个拦截器,放行
    //return false; 不执行下一个拦截器
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("=======处理前=======");
        return false;
    }
    //日志
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("=======处理后=======");
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("=======清理=======");
    }
}

2.2 在springmvc配置文件中配置拦截器

<!--拦截器配置-->
<mvc:interceptors>
    <mvc:interceptor>
        <!--包括这个请求下面的所有请求-->
        <mvc:mapping path="/**"/>
        <bean class="com.lv.config.MyInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

2.3 在控制层新建一个Controller

package com.lv.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @GetMapping("/t1")
    public String test(){
        System.out.println("TestController==>test()方法执行了");
        return "OK";
    }
}

2.4 启动Tomcat测试

2.5 查看后台输出

3 拦截器实现登录验证

3.1 编写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>

3.2 编写登录页面

<%@ 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" required/>
    密码:<input type="text" name="password" required/>
    <input type="submit">
</form>
</body>
</html>

3.3 编写首页

<%@ 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>

3.4 创建一个Controller

package com.lv.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("/goLogin")
    public String login(){
        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("/main")
    public String main(){
        return "main";
    }
    @RequestMapping("/goOut")
    public String goOut(HttpSession session){
        session.removeAttribute("userLoginInfo");
        return "login";
    }
}

3.5 编写用户登录拦截器

package com.lv.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    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;
    }
}

3.6 在springmvc配置文件中配置拦截器

<!--拦截器配置-->
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/user/**"/>
        <bean class="com.lv.config.LoginInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

3.7 启动Tomcat测试

posted @ 2022-01-29 10:28  从0开始丿  阅读(62)  评论(0编辑  收藏  举报