• SpringBoot初级开发--服务请求(GET/POST)所有参数的记录管理(8)



      服务端在定位错误的时候,有时候要还原现场,这就要把当时的所有入参参数都能记录下来,GET还好说,基本NGINX都会记录。但是POST的请求参数基本不会被记录,这就需要我们通过一些小技巧来记录这些参数,放入日志,这里我们通过自定义拦截器来做这个操作。

    1.编写自定义拦截器

    我们紧接上一章的工程源码来做

    package com.example.firstweb.interceptor;
    
    import com.example.firstweb.util.Constants;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.Enumeration;
    
    @Component
    public class AccessInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
            String queryPath = request.getContextPath() + request.getServletPath();
            StringBuffer alldata=new StringBuffer();
            alldata.append("http://" + request.getServerName() + ":" + request.getServerPort()+ queryPath);
    
    		//get请求
            if (null != request.getQueryString()) {
                alldata.append("?" + request.getQueryString());
                alldata.append(" ");
            }else{
                alldata.append(" ");
            }
    
    
            //POST请求
            Enumeration<String> keys = request.getParameterNames();
    
            if (null != keys) {
                while (keys.hasMoreElements()) {
                    String key = keys.nextElement();
                    String value = request.getParameter(key);
    
                    alldata.append(key + "=" + value + "&");
                }
            }
    
            Constants.LOG_ACCESS_INFO.info(alldata.toString());
            alldata.setLength(0);
            return true;
        }
    
    }
    
    
    • 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

    2.配置自定义拦截器

    package com.example.firstweb.config;
    
    import com.example.firstweb.interceptor.AccessInterceptor;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.method.support.HandlerMethodArgumentResolver;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        @Resource
        private AccessInterceptor accessInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            // 自定义拦截器,添加拦截路径和排除拦截路径
            registry.addInterceptor(accessInterceptor).addPathPatterns("/**")
                    .excludePathPatterns("/hello"); // 排除某些不需要拦截的请求url(即带有/hello请求不会被拦截)
        }
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //        需要配置1:----------- 需要告知系统,这是要被当成静态文件的!
    //        第一个方法设置访问路径前缀,第二个方法设置资源路径
            registry.addResourceHandler("/**").addResourceLocations("classpath:");
            registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
            registry.addResourceHandler("/images/**").addResourceLocations("classpath:/images/");
            registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
            registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
        }
    
    }
    
    • 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

    然后在从welcome的控制器里面增加一个post方法

    package com.example.firstweb.controller;
    
    
    import com.example.firstweb.exception.CommException;
    import com.example.firstweb.model.po.WelcomePo;
    import com.example.firstweb.model.vo.WelcomeVo;
    import com.example.firstweb.service.WelcomeService;
    import com.example.firstweb.util.Constants;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    
    
    import org.apache.log4j.Logger;
    import org.springframework.beans.BeanUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @Api(value = "welcome controller", tags = "欢迎界面")
    public class Welcome {
    
        @Autowired
        private WelcomeService welcomeService;
    
        private static final Logger log = Logger.getLogger(Welcome.class);
    
        @GetMapping("/welcomeindex")
        @ApiOperation("欢迎首页的方法1")
        public ModelAndView welcomeIndex(){
    
            ModelAndView view = new ModelAndView("welcomeindex");
    
    
            WelcomePo wpo= welcomeService.getWelcomInfo();
    
            WelcomeVo wvo= new WelcomeVo();
    
            BeanUtils.copyProperties(wpo, wvo);
    
            view.addObject("welcomedata", wvo);
    
    
            //默认控制台输出日志
            log.info("default log info ");
    
            //输出访问日志
            Constants.LOG_ACCESS_INFO.info("welcome index accesss");
            //输出用户阅读日志
            Constants.LOG_USER_READ.info("first user access log ");
    
            return view;
        }
        @PostMapping("/welcomeindex2")
        @ApiOperation("欢迎首页的方法2")
        public void welcomeIndex2(@ApiParam("定制欢迎词") String test){
        }
    }
    
    
    • 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

    启动整个工程代码,然后用postman分别GET访问http://localhost:8088/welcomeindex?name=sss&sho=kow 用post访问http://localhost:8088/welcomeindex2
    然后在日志文件access.log,分别找到两条日志
    在这里插入图片描述
    在这里插入图片描述
    程序的源码可以在这里获得链接: https://pan.baidu.com/s/1v23PyXwB4kvxd79jgLurHw 提取码: mkwc

  • 相关阅读:
    CSS - 移动端布局(二)移动端适配
    贪心算法的高逼格应用——Huffman编码
    勇立潮头!高品质SFT语音数据实现Zero-Shot语音复刻大模型
    Join and meet
    java计算机毕业设计社区人员管理系统源码+系统+mysql数据库+lw文档+部署
    SpringBoot 概念、优点以及网页版创建
    Blazor双向绑定
    猿创征文|AnimeGANv2 照片动漫化:如何基于 PyTorch 和神经网络给 GirlFriend 制作漫画风头像?
    Hugging Face x LangChain: 全新 LangChain 合作伙伴包
    记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
  • 原文地址:https://blog.csdn.net/m0_37239002/article/details/132578289