springboot添加拦截器

1,在annotation里面创建java类,这里创建的文件名为InterceptorUtil

bash 复制代码
package org.appcenter.wx.annotation;


import org.appcenter.db.service.VisitWebsiteService;
import org.appcenter.wx.service.UserTokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

public class InterceptorUtil implements HandlerInterceptor {
    @Autowired
    VisitWebsiteService visitWebsiteService;


    /**
     * 添加访客记录,同一个用户一天算一条记录,所有未登录的用户算一条
     */
     //在业务处理器处理请求之前被调用。预处理,可以进行编码、安全控制、权限校验等处理;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String appcenterToken = request.getHeader("Token");
        Integer userId;
        if (appcenterToken == null || appcenterToken.isEmpty()) {
            userId = 0;
        } else {
            userId = UserTokenManager.getUserId(appcenterToken);
            if (userId == null) {
                userId = 0;
            }

        }
        visitWebsiteService.addVisitors(userId);

        return true;
    }
    
    //在业务处理器处理请求执行完成后,生成视图之前执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {


    }

   //DispatcherServlet完全处理完请求后被调用,可用于清理资源等。返回处理(已经渲染了页面);
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {


    }
}

2,在config里创建类MyWebAppConfigurer 并实现WebMvcConfigurer

bash 复制代码
package org.appcenter.wx.config;

import org.appcenter.wx.annotation.InterceptorUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;

//@EnableWebMvc
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {

    @Bean
    public HandlerInterceptor getMyInterceptor() {
        return new InterceptorUtil();
    }

    //需要拦截的路径,/**表示需要拦截所有请求
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        //注册一个拦截器
        registry.addInterceptor(getMyInterceptor())
                .addPathPatterns("/app/ad/**")
                .addPathPatterns("/app/homePage/**")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html", "/swagger-ui.html/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
相关推荐
找不到、了23 分钟前
Spring-Beans的生命周期的介绍
java·开发语言·spring
caihuayuan441 分钟前
React Native 0.68 安装react-native-picker报错:找不到compile
java·大数据·sql·spring·课程设计
爱编程的鱼1 小时前
C#接口(Interface)全方位讲解:定义、特性、应用与实践
java·前端·c#
旋风菠萝1 小时前
深入理解Java中的Minor GC、Major GC和Full GC
java·jvm·gc
苹果酱05671 小时前
React方向:react脚手架的使用
java·vue.js·spring boot·mysql·课程设计
找不到、了1 小时前
JVM如何处理多线程内存抢占问题
java·jvm
zhougl9962 小时前
Apache HttpClient 5 用法-Java调用http服务
java·http·apache
spjhandsomeman2 小时前
各个历史版本mysql/tomcat/Redis/Jdk/Apache/gitlab下载地址
java·redis·mysql·jdk·tomcat·gitlab
未来影子2 小时前
面试中的线程题
java·数据库·面试
为美好的生活献上中指2 小时前
java每日精进 5.18【文件存储】
java·开发语言·minio·七牛云存储·s3·七牛云