Helius
2021-06-16 5728be2af515b2200e782aa201ca5d4d67d9ea47
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.ibeetl.admin.core.conf;
 
import java.lang.reflect.Method;
import java.util.Date;
 
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreAudit;
import com.ibeetl.admin.core.entity.CoreFunction;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.service.CoreAuditService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.FunctionLocal;
import com.ibeetl.admin.core.util.HttpRequestLocal;
import com.ibeetl.admin.core.util.PlatformException;
 
@Aspect
@Component
public class RbacAnnotationConfig {
    @Autowired
    CorePlatformService platformService;
    @Autowired
    CoreAuditService sysAuditService;
    @Autowired
    HttpRequestLocal httpRequestLocal;
 
    @Autowired
    Environment env;
 
    ObjectMapper jsonMapper = new ObjectMapper();
    private final Log log = LogFactory.getLog(this.getClass());
 
    @org.aspectj.lang.annotation.Around("within(@org.springframework.stereotype.Controller *) && @annotation(function)")
    public Object functionAccessCheck(final ProceedingJoinPoint pjp, Function function) throws Throwable {
        // debug
        String funCode = null;
        CoreUser user = null;
        Method m = null;
        try {
            
            if (function != null) {
                funCode = function.value();
                user = platformService.getCurrentUser();
                Long orgId = platformService.getCurrentOrgId();
                boolean access = platformService.canAcessFunction(user.getId(), orgId, funCode);
                if (!access) {
                    log.warn(jsonMapper.writeValueAsString(user) + "试图访问未授权功能 " + funCode);
                    throw new PlatformException("试图访问未授权功能");
                }
                FunctionLocal.set(funCode);
            }
            
            Object o = pjp.proceed();
            if (function != null) {
                MethodSignature ms = (MethodSignature)pjp.getSignature();
                m = ms.getMethod();
                createAudit(funCode,function.name(), user, true, "",m);
            }
            return o;
 
        } catch (Throwable e) {
            if (function != null) {
                createAudit(funCode, function.name(),user, false, e.getMessage(),m);
            }
            throw e;
        }
 
    }
 
    private void createAudit(String functionCode, String functionName,CoreUser user, boolean success, String msg, Method m) {
        boolean enable = env.getProperty("audit.enable", Boolean.class, false);
        if (!enable) {
            return;
        }
        if(filter(m,functionCode)){
            return ;
        }
        
        CoreAudit audit = new CoreAudit();
        if(StringUtils.isEmpty(functionName)) {
            CoreFunction fun = this.platformService.getFunction(functionCode);
 
            if (fun == null) {
                // 没有在数据库定义,但写在代码里了
                log.warn(functionCode + " 未在数据库里定义");
                functionName = "未定义";
            } else {
                functionName = fun.getName();
            }
        }
        audit.setCreateTime(new Date());
        audit.setFunctionCode(functionCode);
        audit.setFunctionName(functionName);
        audit.setUserId(user.getId());
        audit.setSuccess(success ? 1 : 0);
        audit.setUserName(user.getName());
        audit.setMessage(msg);
        
        audit.setIp(httpRequestLocal.getRequestIP());
        sysAuditService.save(audit);
    }
    
    private boolean filter(Method m,String functionCode){
        if(functionCode.startsWith("audit.")){
            return true;
        }
        String uri = httpRequestLocal.getRequestURI();
        if(uri!=null&&uri.endsWith("/index/condition.json")){
            
            return true ;
        }else{
            return false;
        }
    }
 
}