springboot+resttemplat+请求拦截器

参考:https://www.cnblogs.com/kaibindirver/p/15527052.html

 

 

 

创建配置文件来注册拦截器

MyMvcConfig
 1 package com.config;
 2 
 3 import com.controle.lanjieqi.LoginHandlerInterceptor;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 7 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 8 
 9 /**
10  * @author: Amim
11  * @year: 2022/9
12  **/
13 @Configuration
14 public class MyMvcConfig implements WebMvcConfigurer {
15 
16     //所有的WebMvcConfigurerAdapter组件都会一起起作用
17     @Bean //将组件注册在容器中
18     public WebMvcConfigurer webMvcConfigurerAdapter(){
19         return new WebMvcConfigurer(){
20 
21             //注册拦截器
22             @Override
23             public void addInterceptors(InterceptorRegistry registry) {
24                 //静态资源; *.css,*.js
25                 //SpringBoot已经做好了静态资源映射
26                 registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**");
27 //                .excludePathPatterns("/index.html","/","/user/login","/static/**","/webjars/**");
28                 // /**  表示拦截所有路径下的所有请求
29 //                registry.addInterceptor(new LoginHandlerInterceptor())
30 //                        .addPathPatterns("/person.html","/Person.html",
31 //                                "/questionnaire.html","/Questionnaire.html",
32 //                                "/result.html","/Result.html");
33             }
34         };
35     }
36 
37 }

创建一个拦截器

LoginHandlerInterceptor
 1 package com.controle.lanjieqi;
 2 
 3 import org.springframework.stereotype.Component;
 4 import org.springframework.web.servlet.HandlerInterceptor;
 5 import org.springframework.web.servlet.ModelAndView;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 /**
11  * @author: Amim
12  * @year: 2022/9
13  **/
14 @Component
15 public class LoginHandlerInterceptor implements HandlerInterceptor {
16 
17     //目标方法执行之前
18     @Override
19     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
20 
21         //--------------------------------------------------------添加请求头,允许跨域----方法一
22 //        setHeader(name, value):如果Header中没有定义则添加,如果已定义则用新的value覆盖原用value值。
23 //        addHeader(name, value):如果Header中没有定义则添加,如果已定义则保持原有value不改变。
24         response.setHeader("Access-Control-Allow-Origin","*");
25         response.setHeader("Access-Control-Allow-Headers", "*");
26         //Access-Control-Allow-Methods: 真实请求允许的方法
27         //Access-Control-Allow-Headers: 服务器允许使用的字段
28         //Access-Control-Allow-Credentials: 是否允许用户发送、处理 cookie
29         //Access-Control-Max-Age: 预检请求的有效期,单位为秒。有效期内,不会重复发送预检请求
30 
31 
32         //获取请求参数
33         Object user5 = request.getQueryString();
34         System.out.println(user5);
35         //获取请求头参数
36         Object user6 = request.getHeader("currentUser");
37         System.out.println(user6);
38         //获取请求方式
39         Object user7 = request.getMethod();
40         System.out.println(user7);
41         //获取请求路径
42         Object user8 = request.getRequestURI();
43         System.out.println(user8);
44 //        //重定向访问路径
45 //        response.sendRedirect("/bbb2");
46 ////        添加请求头
47 //        response.addHeader("location","http://baidu.com");
48 //        if (user == null) {
49 //            //未登录,返回登录页面
50 //            System.out.println("第一拦截了,请求前");
51 ////            response.sendRedirect("/LoginError.html");
52 //            return false;
53 //        }else {
54 //            //放行
55         return true;
56 //        }
57     }
58 
59     @Override
60     public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
61         System.out.println("第二拦截了,请求中");
62     }
63 
64     @Override
65     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
66         System.out.println("第三拦截了,请求后");
67     }
68 }

 

posted @ 2022-10-14 13:44  amim  阅读(96)  评论(0编辑  收藏  举报