在web开发的过程中,为了实现登录权限验证,安全验证等,我们往往需要添加一个拦截器在用户的的请求到达controller层的时候实现登录验证,那么SpringBoot如何添加拦截器呢?
一:添加拦截器
打开core→configurer→WebConfigurer
添加如下
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 1/**
2 * TODO 修改为自己的需求
3 */
4private static final String IZATION = "CHUCHEN";
5
6/**
7 * 添加拦截器 请求头拦截
8 */
9@Override
10public void addInterceptors(InterceptorRegistry registry) {
11 registry.addInterceptor(
12 //注意,HandlerInterceptorAdapter 这里可以修改为自己创建的拦截器
13 new HandlerInterceptorAdapter() {
14 @Override
15 public boolean preHandle(HttpServletRequest request,
16 HttpServletResponse response, Object handler) throws Exception {
17 String ization = request.getHeader("ization");
18 if(IZATION.equals(ization)){
19 return true;
20 }else{
21 RetResult<Object> result = new RetResult<>();
22 result.setCode(RetCode.UNAUTHORIZED).setMsg("签名认证失败");
23 responseResult(response, result);
24 return false;
25 }
26 }
27 }
28 //这里添加的是拦截的路径 /**为全部拦截
29 ).addPathPatterns("/userInfo/selectAlla");
30}private void responseResult(HttpServletResponse response, RetResult<Object> result) {
31 response.setCharacterEncoding("UTF-8");
32 response.setHeader("Content-type", "application/json;charset=UTF-8");
33 response.setStatus(200);
34 try {
35 response.getWriter().write(JSON.toJSONString(result, SerializerFeature.WriteMapNullValue));
36 } catch (IOException ex) {
37 LOGGER.error(ex.getMessage());
38 }
39}
40
二:测试
输入localhost:8080/userInfo/selectAll
输入localhost:8080/userInfo/selectAlla
在header中添加签名
三:自己创建拦截器
创建core→interceptor→Interceptor1
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 1package com.example.demo.core.interceptor;
2
3import org.springframework.web.servlet.HandlerInterceptor;
4import org.springframework.web.servlet.ModelAndView;
5
6import javax.servlet.http.HttpServletRequest;
7import javax.servlet.http.HttpServletResponse;
8
9public class Interceptor1 implements HandlerInterceptor {
10
11 /**
12 * 在请求处理之前进行调用(Controller方法调用之前)
13 * @param request
14 * @param response
15 * @param handler
16 * @return
17 * @throws Exception
18 */
19 @Override
20 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
21 throws Exception {
22 System.out.println(">>>MyInterceptor1>>>>>>> preHandle");
23 // 只有返回true才会继续向下执行,返回false取消当前请求
24 return true;
25 }
26
27 /**
28 * 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
29 * @param request
30 * @param response
31 * @param handler
32 * @param modelAndView
33 * @throws Exception
34 */
35 @Override
36 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
37 ModelAndView modelAndView) throws Exception {
38 System.out.println(">>>MyInterceptor1>>>>>>> postHandle");
39 }
40
41 /**
42 * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
43 * @param request
44 * @param response
45 * @param handler
46 * @param ex
47 * @throws Exception
48 */
49 @Override
50 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
51 throws Exception {
52 System.out.println(">>>MyInterceptor1>>>>>>> afterCompletion");
53 }
54}
55
四:修改WebConfigurer配置
修改addInterceptors 方法如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 1@Override
2public void addInterceptors(InterceptorRegistry registry) {
3 registry.addInterceptor(
4 //注意,HandlerInterceptorAdapter 这里可以修改为自己创建的拦截器
5 new Interceptor1() {
6 @Override
7 public boolean preHandle(HttpServletRequest request,
8 HttpServletResponse response, Object handler) throws Exception {
9 String ization = request.getHeader("ization");
10 if(IZATION.equals(ization)){
11 return true;
12 }else{
13 RetResult<Object> result = new RetResult<>();
14 result.setCode(RetCode.UNAUTHORIZED).setMsg("签名认证失败");
15 responseResult(response, result);
16 return false;
17 }
18 }
19 }
20 //这里添加的是拦截的路径 /**为全部拦截
21 ).addPathPatterns("/userInfo/selectAlla");
22}
23
五:测试
输入localhost:8080/userInfo/selectAlla
在header中添加签名
成功