在项目的系统中,需要写一个日志管理,即将每次用户的增删改操作都存储于数据库中,此时,通过拦截器来设置,通过判断用户的请求,由于项目符合restful
风格,但请求method
为put
、delete
或post
时,获取用户信息及请求信息,插入数据库中
首先是定义一个拦截器
/**
* @author fuzihao
* @date 2019/8/14 16:03
*/
public class OperationLogInterceptor extends HandlerInterceptorAdapter {
@Autowired
private ILogService logService;
@Autowired
private IElementService elementService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(handler instanceof HandlerMethod){
String methodType = request.getMethod();
System.out.println(request.getRequestURI());
//如果是post、delete或put
if(StringUtils.equalsIgnoreCase(HttpMethodEnum.POST.