zhuo
2024-02-28 d21be5568445e5e1bc887d9980c28bf248761410
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.yuanchu.mom.config;
 
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.SystemLogMapper;
import com.yuanchu.mom.pojo.SystemLog;
import io.swagger.annotations.ApiOperation;
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.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
 
/**
 * @Author 戴卓
 * @Date 2024/2/27
 */
@Component
public class LogConfig implements HandlerInterceptor {
 
    @Resource
    private SystemLogMapper systemLogMapper;
 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
 
        if (handler instanceof HandlerMethod) {
            if (request.getRequestURL().toString().contains("/error") ||
                    request.getRequestURL().toString().contains("/outPath") ||
                    request.getRequestURL().toString().contains("/user/enter")) {
                return HandlerInterceptor.super.preHandle(request, response, handler);
            }
            HandlerMethod h = (HandlerMethod)handler;
            Method method = h.getMethod();
            ApiOperation annotation = method.getAnnotation(ApiOperation.class);
            if (annotation == null) {
                return HandlerInterceptor.super.preHandle(request, response, handler);
            }
            SystemLog systemLog = new SystemLog();
            systemLog.setMethod(method.getName());
            systemLog.setMethodName(annotation.value());
            systemLogMapper.insert(systemLog);
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
 
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }
 
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}