0%

SSM-12-拦截器

SSM-12-拦截器

  1. 权限验证
  2. 记录请求信息日志
  3. 判断用户是否登录

拦截器的配置

1
2
3
<mvc:interceptors>

</mvc:interceptors>
  • 全局拦截器
  • 拦截指定路径
  • 不需要拦截的路径
  • /hello 拦截以/hello结尾的路径

拦截器的执行流程

单个拦截器的执行流程

  1. 配置jar包
  2. 配置web.xml
  3. 配置控制器
1
2
3
4
5
6
7
8
9
10
11
12

@Controller
public class HelloController {
/*
* 页面
*/
@RequestMapping("/hello")
public String hello() {
System.out.println("Hello");
return "success";
}
}
  1. 配置拦截器
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
34
35
36
package com.itheima.intercepter;

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

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

/**
* @author NoOne dotaerday@gmail.com:
* @version 创建时间:2020年3月12日 上午8:50:27 类说明
*/

/*
* 实现了HandlerInterceptor的接口的自定义拦截器
*/
public class CustomeInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
System.out.println("CustomInterceptor... preHandle");
return true;
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("CustomInterceptor..post");
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
throws Exception {
System.out.println("CustomInterceptor..after");
}

}

配置springmvc-config里的拦截器

1
2
3
4
5
6

<!-- 配置拦截器 -->
<mvc:interceptors>
<!-- 使用bean直接定义拦截器 -->
<bean class="com.itheima.intercepter.CustomeInterceptor"/>
</mvc:interceptors>

运行

1
2
3
4
CustomInterceptor... preHandle
Hello
CustomInterceptor..post
CustomInterceptor..after

确实是,先preHandle,然后post,然后after

多个拦截器的执行流程

复制两份interceptor

springmvc-config配置,

1
2
3
4
5
6
7
8
9
10
11
<!-- 拦截器1 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.itheima.intercepter.Interceptor1" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/hello" />
<bean class="com.itheima.intercepter.Interceptor2" />
</mvc:interceptor>
</mvc:interceptors>

这里我出了一个错,interceptors跟interceptor不一样,差点就报错了,在mvc:interceptors里配多个拦截器

执行结果

1
2
3
4
5
6
7
CustomInterceptor1... preHandle
CustomInterceptor2... preHandle
Hello
CustomInterceptor2..post
CustomInterceptor1..post
CustomInterceptor2..after
CustomInterceptor1..after

类似于栈的结构,首先1压栈,2压栈,然后将2出栈,1 出栈,最后释放空间

拦截器的应用—用户登录权限验证

控制器

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
34
35
36
37
38
39
40
@Controller
public class UserController {
@RequestMapping(value="/login", method=RequestMethod.GET)
public String toLogin() {
return "login";
}

/*
* 登录
*/
@RequestMapping(value="/login",method=RequestMethod.POST)
public String login(User user, Model model, HttpSession session) {
//获取用户密码
String username = user.getUsername();
String password = user.getPassword();
if(username != null && username.equals("xiaoming") && password.equals("123456")) {
session.setAttribute("USER_SESSION", user);
//重定向到主页面的跳转方法
return "redirect:main";
}
model.addAttribute("msg", "用户名或者密码错误");
return "login";
}


/*
* 向用户主页面跳转
*/
@RequestMapping("/main")
public String toMain() {
return "main";
}

@RequestMapping("/Logout")
public String logout(HttpSession session) {
//清除session
session.invalidate();
return "redirect: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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.itheima.intercepter;

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

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

import com.itheima.po.User;

/**
* @author NoOne dotaerday@gmail.com:
* @version 创建时间:2020年3月12日 上午9:30:10
* 类说明
*/

public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
//获取的请求的URL
String url = request.getRequestURI();
//URL除了login.jsp是公开的,其余都拦截
if(url.indexOf("/login") >= 0) {
return true;
}
HttpSession session = request.getSession();
User user = (User) session.getAttribute("USER_SESSION");
//如果有则返回true
if(user != null) {
return true;
}
request.setAttribute("msg", "您还没有登录,请先登录!");
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
return false;
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
throws Exception {

}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
throws Exception {

}

}

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.itheima.intercepter.Interceptor1" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/hello" />
<bean class="com.itheima.intercepter.Interceptor2" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.itheima.intercepter.LoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors>

main.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>系统主页</title>
</head>
<body>
当前用户: ${USER_SESSION.username }
<a href="${pageContext.request.contextPath }/Logout"/>
</body>
</html>

login.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
${msg}
<form action="${pageContext.request.contextPath }/login"
method="POST">
用户名:<input type="text" name="username"/><br />
密&nbsp;&nbsp;&nbsp;码:
<input type="password" name="password"/><br />
<input type="submit" value="登录" />
</form>
</body>
</html>

又出现了好多错

  1. 拦截器配置错误,然后报 root 的错误
  2. 拦截器名称配置错误,报class not found

测试结果

1
2
3
4
5
6
CustomInterceptor1... preHandle
CustomInterceptor1..post
CustomInterceptor1..after
CustomInterceptor1... preHandle
CustomInterceptor1..post
CustomInterceptor1..after

本文作者:NoOne
本文地址https://noonegroup.xyz/posts/12321d7f/
版权声明:转载请注明出处!