cl
11 小时以前 c4bba32b20f0869b45ed14be04543869dd91ee6c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.vincent.rsf.server.system.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.httpaudit.entity.HttpAuditRule;
import com.vincent.rsf.httpaudit.service.HttpAuditRuleService;
import com.vincent.rsf.server.common.domain.BaseParam;
import com.vincent.rsf.server.common.domain.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
 
import org.apache.commons.lang3.StringUtils;
 
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
 
@RestController
public class HttpAuditRuleController extends BaseController {
 
    private static final Set<String> RULE_TYPES = new HashSet<>(Arrays.asList(
            HttpAuditRule.TYPE_URI, HttpAuditRule.TYPE_IP, HttpAuditRule.TYPE_REQUEST_BODY));
    private static final Set<String> MATCH_MODES = new HashSet<>(Arrays.asList(
            HttpAuditRule.MODE_EQUAL, HttpAuditRule.MODE_PREFIX, HttpAuditRule.MODE_CONTAINS, HttpAuditRule.MODE_REGEX));
 
    @Autowired
    private HttpAuditRuleService httpAuditRuleService;
 
    @PreAuthorize("hasAuthority('system:httpAuditRule:list')")
    @PostMapping("/httpAuditRule/page")
    public R page(@RequestBody Map<String, Object> map) {
        BaseParam baseParam = buildParam(map, BaseParam.class);
        PageParam<HttpAuditRule, BaseParam> pageParam = new PageParam<>(baseParam, HttpAuditRule.class);
        QueryWrapper<HttpAuditRule> wrapper = pageParam.buildWrapper(true, qw -> {
            qw.orderByAsc("sort_order").orderByAsc("id");
        }, "create_time");
        Page<HttpAuditRule> page = httpAuditRuleService.page(pageParam, wrapper);
        return R.ok().add(page);
    }
 
    @PreAuthorize("hasAuthority('system:httpAuditRule:list')")
    @GetMapping("/httpAuditRule/{id}")
    public R get(@PathVariable Long id) {
        return R.ok().add(httpAuditRuleService.getById(id));
    }
 
    @PreAuthorize("hasAuthority('system:httpAuditRule:save')")
    @PostMapping("/httpAuditRule/save")
    public R save(@RequestBody HttpAuditRule rule) {
        normalizeRecordAllRule(rule);
        R err = validate(rule);
        if (err != null) {
            return err;
        }
        Date now = new Date();
        if (rule.getEnabled() == null) {
            rule.setEnabled(1);
        }
        if (rule.getSortOrder() == null) {
            rule.setSortOrder(0);
        }
        if (StringUtils.isBlank(rule.getDirection())) {
            rule.setDirection(HttpAuditRule.DIR_IN);
        }
        rule.setCreateTime(now);
        rule.setUpdateTime(now);
        if (httpAuditRuleService.save(rule)) {
            httpAuditRuleService.refreshCache();
            return R.ok("Save Success").add(rule);
        }
        return R.error("Save Fail");
    }
 
    @PreAuthorize("hasAuthority('system:httpAuditRule:update')")
    @PostMapping("/httpAuditRule/update")
    public R update(@RequestBody HttpAuditRule rule) {
        normalizeRecordAllRule(rule);
        R err = validate(rule);
        if (err != null) {
            return err;
        }
        if (rule.getId() == null) {
            return R.error("id required");
        }
        if (rule.getEnabled() == null) {
            rule.setEnabled(1);
        }
        if (rule.getSortOrder() == null) {
            rule.setSortOrder(0);
        }
        if (StringUtils.isBlank(rule.getDirection())) {
            rule.setDirection(HttpAuditRule.DIR_IN);
        }
        rule.setUpdateTime(new Date());
        if (httpAuditRuleService.updateById(rule)) {
            httpAuditRuleService.refreshCache();
            return R.ok("Update Success").add(rule);
        }
        return R.error("Update Fail");
    }
 
    @PreAuthorize("hasAuthority('system:httpAuditRule:remove')")
    @PostMapping("/httpAuditRule/remove/{ids}")
    public R remove(@PathVariable Long[] ids) {
        if (httpAuditRuleService.removeByIds(Arrays.asList(ids))) {
            httpAuditRuleService.refreshCache();
            return R.ok("Remove Success");
        }
        return R.error("Remove Fail");
    }
 
    private static void normalizeRecordAllRule(HttpAuditRule rule) {
        if (rule == null || rule.getRecordAll() == null || rule.getRecordAll() != 1) {
            return;
        }
        if (StringUtils.isBlank(rule.getRuleType())) {
            rule.setRuleType(HttpAuditRule.TYPE_URI);
        }
        if (StringUtils.isBlank(rule.getMatchMode())) {
            rule.setMatchMode(HttpAuditRule.MODE_EQUAL);
        }
        if (StringUtils.isBlank(rule.getPattern())) {
            rule.setPattern("*");
        }
        if (StringUtils.isBlank(rule.getDirection())) {
            rule.setDirection(HttpAuditRule.DIR_BOTH);
        }
    }
 
    private static R validate(HttpAuditRule rule) {
        if (rule == null) {
            return R.error("body required");
        }
        if (StringUtils.isBlank(rule.getRuleType()) || !RULE_TYPES.contains(rule.getRuleType())) {
            return R.error("ruleType invalid");
        }
        if (StringUtils.isBlank(rule.getMatchMode()) || !MATCH_MODES.contains(rule.getMatchMode())) {
            return R.error("matchMode invalid");
        }
        if (StringUtils.isBlank(rule.getPattern())) {
            return R.error("pattern required");
        }
        String dir = rule.getDirection();
        if (StringUtils.isNotBlank(dir)) {
            if (!Arrays.asList(HttpAuditRule.DIR_IN, HttpAuditRule.DIR_OUT, HttpAuditRule.DIR_BOTH).contains(dir)) {
                return R.error("direction invalid");
            }
        }
        return null;
    }
}