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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 1package com.xxx.interceptor;
2
3import org.springframework.stereotype.Component;
4import org.springframework.web.servlet.HandlerInterceptor;
5import org.springframework.web.servlet.ModelAndView;
6
7import javax.servlet.http.HttpServletRequest;
8import javax.servlet.http.HttpServletResponse;
9import javax.servlet.http.HttpSession;
10import java.io.PrintWriter;
11
12/**
13 * 登录拦截器
14 */
15@Component
16public class SessionInterceptor implements HandlerInterceptor {
17
18 /**
19 * 该方法将在请求处理之前进行调用,返回true会执行下一个Interceptor,
20 * 返回false则不会执行下一个Interceptor也不会执行Controller里的方法,
21 * 先声明的Interceptor的preHandle方法会先执行。
22 *
23 * @param request
24 * @param response
25 * @param handler
26 * @return
27 * @throws Exception
28 */
29 @Override
30 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
31 System.out.println("SessionInterceptor preHandle");
32 HttpSession session = request.getSession(false);
33 if (session != null && session.getAttribute("user") != null) {
34 return true;
35 } else {
36 PrintWriter printWriter = response.getWriter();
37 printWriter.write("{code: 501, message:'not login!'}");
38 return false;
39 }
40 }
41
42 /**
43 * 该方法将在preHandle方法返回为true,
44 * 并且当前请求进行处理之后调用,
45 * 先声明的Interceptor的postHandle方法会后执行
46 *
47 * @param request
48 * @param response
49 * @param handler
50 * @param modelAndView
51 * @throws Exception
52 */
53 @Override
54 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
55 System.out.println("SessionInterceptor postHandle");
56 }
57
58 /**
59 * 该方法将在preHandle方法返回为true,
60 * 并且当前请求进行处理之后调用,
61 * 一般用于进行资源清理
62 * @param request
63 * @param response
64 * @param handler
65 * @param ex
66 * @throws Exception
67 */
68 @Override
69 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
70 System.out.println("SessionInterceptor afterCompletion");
71 }
72}
73
74
2.将其添加到拦截配置类中
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 1package com.xxx.config;
2
3import com.xxx.interceptor.SessionInterceptor;
4import org.springframework.beans.factory.annotation.Autowired;
5import org.springframework.context.annotation.Configuration;
6import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
7import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
8import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
9
10@Configuration
11public class WebMvcConfig implements WebMvcConfigurer {
12
13 @Autowired
14 private SessionInterceptor sessionInterceptor;
15
16 @Override
17 public void addInterceptors(InterceptorRegistry registry) {
18 registry.addInterceptor(sessionInterceptor)
19 // 设置拦截所有路径地址
20 .addPathPatterns("/**")
21 // 设置过滤不需要拦截的路径地址,尤其是登录地址一定要加上
22 .excludePathPatterns("/user/login", "/error");
23 }
24
25}
26
27
此时登录拦截器就配置好了,除了请求 /user/login 和/error 的请求都会被拦截器拦截
3.添加请求映射方法
在上面的WebMvcConfig类中添加
1
2
3
4
5
6 1@Override
2 public void addViewControllers(ViewControllerRegistry registry) {
3 // 配置请求映射跳转路径
4 registry.addViewController("/hi").setViewName("hi.html");
5 }
6
所有请求对应的地址就可以直接跳转到指定页面中,减少冗余页面跳转代码。
4.添加静态文件访问路径
在上面的WebMvcConfig类中添加
1
2
3
4
5
6
7
8
9 1 /**
2 * 添加静态文件访问路径
3 * @param registry
4 */
5 @Override
6 public void addResourceHandlers(ResourceHandlerRegistry registry) {
7 registry.addResourceHandler("/images/**").addResourceLocations("classpath:/images/");
8 }
9