1.自定义注解Log
import java.lang.annotation.*;
/**
* description: 操作日志记录
* @author dongqing
* 2021/8/7 12:35
*
* @version 1.0.0
*/
@Target({ElementType.METHOD})
//如果不写,默认为RetentionPolicy.CLASS,另外源注解上的Retention对本注解无效
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {
String url();
String invokeMethod();
String operation();
}
2.自定义Interceptor
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* description: 自定义日志拦截器
* @author dongqing
* 2021/8/7 13:23
*
* @version 1.0.0
*/
@Slf4j
@Component
public class LogInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("进入了log拦截器......");
String queryString = request.getQueryString();
log.info("queryString==={}",queryString);
HandlerMethod handlerMethod = (HandlerMethod) handler;
Log methodAnnotation = handlerMethod.getMethodAnnotation(Log.class);
if(methodAnnotation != null ){
// 如果自己拥有OpCommonRecord标注 就获取注解的值
String url = methodAnnotation.url();
String invokeMethod = methodAnnotation.invokeMethod();
String operation = methodAnnotation.operation();
log.info("url====={},invokeMethod=======>{},operation=======>{}",url,invokeMethod,operation);
//TODO 处理注解拦截到的相关值,如:存储日志到数据库
return true;
}else {
log.info("此方法没有Log注解");
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
3.注入拦截器
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* description:注入自定义拦截器到spring中
* dongqing
* 2021/8/6 9:10
*
* @version 1.0.0
*/
//可extends WebMvcConfigurationSupport,但是测试时发现拦截无效,使用implement WebMvcConfigurer时就能生效,大概是与版本有关;同时记得添加@Configuration注解
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(logInterceptor()).addPathPatterns("/**");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//加入静态方法的拦截,根据实际需要添加 registry.addResourceHandler("doc.html").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/");
}
//如果LogInterceptor中有相关属性,需要以下代码,否则无法读取到属性值
@Bean
public LogInterceptor logInterceptor() {
return new LogInterceptor();
}
}
4.编写controller
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import wangjubao.base.common.extend.Response;
/**
* 用户登录
*
* @author dongqing
*/
@RestController
@Api(value = "用户登录")
public class UserController {
@PostMapping("/login")
//添加注解,实现拦截
@Log(url = "/login", invokeMethod = "userLogin(@RequestBogy User user)", operation = "用户登录")
public Response login(@RequestBody User user) {
Response response = new Response();
//TODO 用户登录信息验证
return response;
}
5.测试
启动项目,在postman上模拟登录请示,观察拦截器是否生成