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 RULE_TYPES = new HashSet<>(Arrays.asList( HttpAuditRule.TYPE_URI, HttpAuditRule.TYPE_IP, HttpAuditRule.TYPE_REQUEST_BODY)); private static final Set 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 map) { BaseParam baseParam = buildParam(map, BaseParam.class); PageParam pageParam = new PageParam<>(baseParam, HttpAuditRule.class); QueryWrapper wrapper = pageParam.buildWrapper(true, qw -> { qw.orderByAsc("sort_order").orderByAsc("id"); }, "create_time"); Page 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; } }