使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)

使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)

问题背景

项目中经常用的拦截器进行请求拦截进行日志统一打印输入输出,介绍一下WebMvcConfigurer拦截器的使用
注意事项:

WebMvcConfigurer拦截器做增强处理

1 添加拦截器的配置类

package com.lanran.webmvcconfigurer.config;

import com.lanran.webmvcconfigurer.interceptor.WebInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Description:
 * @Created: IDEA2021
 * @author: 蓝染
 * @createTime: 2022-07-30 17:57
 **/

@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    
    
        registry.addInterceptor(new WebInterceptor()) //注册拦截器,
                .addPathPatterns("/**");  //所有请求都需要拦截
    }
}

2 添加拦截器

package com.lanran.webmvcconfigurer.interceptor;


import com.lanran.webmvcconfigurer.entity.UserInfoTo;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


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



/**
 * @Description: 在执行目标方法之前,判断用户的登录状态.是临时用户,还是登录用户,并封装传递给controller目标请求
 * @Created: IDEA2021
 * @author: 蓝染
 * @createTime: 2022-07-30 17:31
 **/
//拦截器需要添加配置才能生效,添加配置类GulimallWebConfig implements WebMvcConfigurer
//HandlerInterceptor这个是MVC的拦截器,所以所有的getmapping和postmapping都会被拦截
public class WebInterceptor implements HandlerInterceptor {
    
    



    /***
     * 目标方法执行之前,这里的目标方法
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    
    

        System.out.println("before");

        return true;
    }


    /**
     * 业务执行之后,分配临时用户来浏览器保存
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    
    

        System.out.println("after");

    }

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

    }
}

3 添加测试接口

package com.lanran.webmvcconfigurer.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author suolong
 * @Date 2022/7/22 9:52
 * @Version 2.0
 */
@RestController
public class WebController {
    
    



    @GetMapping("/test")
    public String test(){
    
    
        System.out.println("test method");
        return "success";
    }

}

4 使用的实体类

package com.lanran.webmvcconfigurer.entity;

import lombok.Data;

/**
 * @Author suolong
 * @Date 2022/7/22 8:44
 * @Version 2.0
 */
@Data
public class UserInfoTo {
    
    

    private String name;

    private String gender;

    private Integer age;

    private Boolean flag;

}

5 启动类

package com.lanran.webmvcconfigurer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebMvcConfigurerApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(WebMvcConfigurerApplication.class, args);
    }

}

6 项目目录

7 启动项项目,使用postman测试接口

日志打印结果

总结

把想拦的拦下来吧

扫描二维码关注公众号,回复: 14400389 查看本文章




作为程序员第 214 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

Lyric: 在有眼泪的雨里

猜你喜欢

转载自blog.csdn.net/cucgyfjklx/article/details/125963331