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

导读:本篇文章讲解 使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

使用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 项目目录
使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)

7 启动项项目,使用postman测试接口
使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)
日志打印结果
使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)

总结

把想拦的拦下来吧

作为程序员第 214 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …
使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)使用WebMvcConfigurer进行接口请求拦截进行中增强(附源码)

Lyric: 在有眼泪的雨里

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/110661.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!