hjg
2024-01-20 4a3404efc438b16044fd9170814e6545a3f86fae
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
package com.mandi.springmvc.logs;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.context.ApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
 
import com.mandi.common.Jacksonmethod;
import com.mandi.common.SessionMethod;
import com.mandi.common.SpringContext;
import com.mandi.system.service.IMOpLogServie;
import com.mandi.system.service.impl.MOpLogServie;
import com.mandi.system.persist.Login;
import com.mandi.system.persist.ModuleEnum;
 
public class LogsInterceptor extends HandlerInterceptorAdapter {
 
    @Override
    public boolean preHandle(HttpServletRequest r,
            HttpServletResponse re, Object handler) throws Exception {
        if(handler.getClass().isAssignableFrom(HandlerMethod.class)){
            OpLogs oplog= ((HandlerMethod)handler).getMethodAnnotation(OpLogs.class);
            if(oplog==null){
                return true;
            }
            ApplicationContext ac= SpringContext.getAppContext();
            if(ac==null){
                return true;
            }
            IMOpLogServie imlser=ac.getBean(MOpLogServie.class);
            Login l=SessionMethod.getlogin(r.getSession());
            if(l==null){
                l=new Login();
                l.setUsername("未登录");
                l.setWorkername(r.getRemoteAddr());
                l.setDepartname("不知道");
            }
            String name=oplog.name();
            ModuleEnum module=oplog.module();
            String note="";
            try{
                note=Jacksonmethod.tojson(r.getParameterMap(), false);
                imlser.insert(l, module, name, note);
            }catch(Exception e){
                e.printStackTrace();
            }
            
        }
        
        return true;
    }
    
}