springboot添加拦截器

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

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

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/");
    }

}

你可能感兴趣的:(spring,boot,后端,java)