1、数据验证拦截器类
package com.yt.interceptor;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 拦截器数据验证
* @author tyg
* @date 2020年8月5日下午3:46:09
*/
@Log4j2
@Component
public class AuthHandlerInterceptor extends HandlerInterceptorAdapter {
private long startTime = 0;
/**
* 请求处理前调用
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
startTime = System.currentTimeMillis();
return super.preHandle(request, response, handler);
}
/**
* 请求处理完后渲染视图前调用
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
log.info("\n======请求地址:{},耗时:{}ms", request.getServletPath(), System.currentTimeMillis() - startTime);
super.postHandle(request, response, handler, modelAndView);
}
/**
* 渲染视图后调用
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
super.afterCompletion(request, response, handler, ex);
}
}
2、将数据验证拦截器类加入到拦截器中
package com.yt.interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import javax.annotation.Resource;
/**
* 加入URL拦截
* @author tyg
* @date 2020-12-31 14:59
*/
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {
@Resource
private AuthHandlerInterceptor authHandlerInterceptor;
/**
* 加入URL拦截
* @param registry
* @author tyg
* @date 2020-12-31 14:59
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authHandlerInterceptor).addPathPatterns("/**");
super.addInterceptors(registry);
}
/**
* 设置默认数据
*/
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
// 默认返回格式 JSON
configurer.defaultContentType(MediaType.APPLICATION_JSON);
}
}
3、第2步中,会存在一个问题,如果在application.yml设置了返回时间格式为如下,是不会生效的,还是会返回时间戳,如果要返回格式化了的时间,那么就要将第2步进行替换。
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
替换为:
package com.yt.interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource;
/**
* 加入URL拦截
* @author tyg
* @date 2020-12-31 14:59
*/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Resource
private AuthHandlerInterceptor authHandlerInterceptor;
/**
* 加入URL拦截
* @param registry
* @author tyg
* @date 2020-12-31 14:59
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authHandlerInterceptor).addPathPatterns("/**");
}
}