|
|
@@ -0,0 +1,220 @@
|
|
|
+package com.chuanghai.travelbigdata.disease.command.controller;
|
|
|
+
|
|
|
+import com.chuanghai.travelbigdata.disease.command.entity.AdminInfoEntity;
|
|
|
+import com.chuanghai.travelbigdata.disease.command.exception.BizCodeEnume;
|
|
|
+import com.chuanghai.travelbigdata.disease.command.exception.RRException;
|
|
|
+import com.chuanghai.travelbigdata.disease.command.service.AdminInfoService;
|
|
|
+import com.chuanghai.travelbigdata.disease.command.utils.CommonResult;
|
|
|
+import com.chuanghai.travelbigdata.disease.command.utils.PageParam;
|
|
|
+import com.chuanghai.travelbigdata.disease.command.utils.PageUtils;
|
|
|
+import com.chuanghai.travelbigdata.disease.command.utils.ParamCheck;
|
|
|
+import com.chuanghai.travelbigdata.disease.command.vo.UserTokenVO;
|
|
|
+import org.assertj.core.util.Arrays;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.validation.BindingResult;
|
|
|
+import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.PutMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestHeader;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.validation.Valid;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+import java.util.concurrent.atomic.LongAdder;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+import java.util.stream.IntStream;
|
|
|
+import java.util.stream.Stream;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 管理员模块
|
|
|
+ * @author codingliang
|
|
|
+ * @email codingliang@gmail.com
|
|
|
+ * @date 2021-01-24 10:05:20
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("adminInfo")
|
|
|
+public class AdminInfoController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AdminInfoService adminInfoService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 管理员登录
|
|
|
+ * @param userName 用户名
|
|
|
+ * @param password 密码
|
|
|
+ * @apiNote 测试用户名密码:admin/123456
|
|
|
+ */
|
|
|
+ @PostMapping("login")
|
|
|
+ public CommonResult<UserTokenVO> login(@RequestParam String userName,
|
|
|
+ @RequestParam String password) {
|
|
|
+ UserTokenVO tokenVO = adminInfoService.login(userName, password);
|
|
|
+ return CommonResult.ok().setResult(tokenVO);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 管理员列表
|
|
|
+ */
|
|
|
+ @GetMapping("/list")
|
|
|
+ public CommonResult<PageUtils<AdminInfoEntity>> list(PageParam pageParam){
|
|
|
+ PageUtils page = adminInfoService.queryPage(pageParam);
|
|
|
+ return CommonResult.ok().setResult(page);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增管理员
|
|
|
+ * @param adminToken 管理员token
|
|
|
+ * @apiNote 只有超级管理员才可以新增管理员
|
|
|
+ */
|
|
|
+ @PostMapping("/save")
|
|
|
+ public CommonResult<String> save(@RequestHeader("Admin-Token") String adminToken,
|
|
|
+ @RequestBody @Valid AdminInfoEntity adminInfo,
|
|
|
+ BindingResult result,
|
|
|
+ HttpServletRequest request){
|
|
|
+ ParamCheck.checkBean(result);
|
|
|
+
|
|
|
+ checkAuth(request);
|
|
|
+
|
|
|
+ String password = adminInfo.getPassword();
|
|
|
+ if (!StringUtils.hasText(password)) {
|
|
|
+ throw new RRException(BizCodeEnume.PARAMETER_ERROR, "密码不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
|
|
+ adminInfo.setPassword(passwordEncoder.encode(password));
|
|
|
+ adminInfoService.save(adminInfo);
|
|
|
+ return CommonResult.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改管理员
|
|
|
+ * @param adminToken 管理员token
|
|
|
+ * @apiNote 只有超级管理员才可以修改管理员
|
|
|
+ */
|
|
|
+ @PutMapping("/update")
|
|
|
+ public CommonResult<String> update(@RequestHeader("Admin-Token") String adminToken,
|
|
|
+ @RequestBody @Valid AdminInfoEntity adminInfo,
|
|
|
+ BindingResult result,
|
|
|
+ HttpServletRequest request){
|
|
|
+ ParamCheck.checkBean(result);
|
|
|
+
|
|
|
+ checkAuth(request);
|
|
|
+
|
|
|
+ adminInfo.setPassword(null);
|
|
|
+ adminInfoService.updateById(adminInfo);
|
|
|
+ return CommonResult.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除管理员
|
|
|
+ * @param adminToken 管理员token
|
|
|
+ * @param id 管理员id
|
|
|
+ * @apiNote 只有超级管理员才可以删除管理员
|
|
|
+ */
|
|
|
+ @DeleteMapping("/delete/{id}")
|
|
|
+ public CommonResult<String> delete(@RequestHeader("Admin-Token") String adminToken,
|
|
|
+ @PathVariable("id") Long id,
|
|
|
+ HttpServletRequest request){
|
|
|
+ Long userId = Long.valueOf(String.valueOf(request.getAttribute("currentUserId")));
|
|
|
+ if (id.longValue() == userId.longValue()) {
|
|
|
+ throw new RRException(BizCodeEnume.PARAMETER_ERROR, "非法操作");
|
|
|
+ }
|
|
|
+
|
|
|
+ checkAuth(request);
|
|
|
+
|
|
|
+ adminInfoService.removeById(id);
|
|
|
+ return CommonResult.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkAuth(HttpServletRequest request) {
|
|
|
+ Long userId = Long.valueOf(String.valueOf(request.getAttribute("currentUserId")));
|
|
|
+ AdminInfoEntity admin = adminInfoService.getById(userId);
|
|
|
+ // adminType 为 0,表示为超级管理员
|
|
|
+ if (admin == null || !"0".equals(admin.getAdminType())) {
|
|
|
+ throw new RRException(BizCodeEnume.PERMISSION_DENIED);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1-10791
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+ String fileName = "E:\\newworkspace\\wangxinan-project\\文档\\wangxinan\\开发文档\\10.193.5.25.log";
|
|
|
+ AtomicInteger i = new AtomicInteger(1);
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+ Stream<String> files = Files.lines(Paths.get(fileName), StandardCharsets.ISO_8859_1);
|
|
|
+ files.forEachOrdered(line -> {
|
|
|
+ try {
|
|
|
+ System.out.println("===================");
|
|
|
+ System.out.println("【" + i.getAndIncrement() + "】" + line);
|
|
|
+// List<String> ips = getInfos(line);
|
|
|
+// System.out.println(String.join(",", ips));
|
|
|
+ System.out.println("===================");
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("发生错误" + e.getMessage());
|
|
|
+ System.out.println("===================");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ System.out.println("处理完成:花费" + (System.currentTimeMillis() - startTime) + "毫秒");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<String> getInfos(String content){
|
|
|
+ // From 1.15.82.77:55945(xethernet1/0) to 42.123.72.136:39474(-), threat name: syn-flood, threat type: Dos, threat subtype: DDoS Flood, App/Protocol: IPv4/TCP, action: DROP, defender: AD, severity: Medium, zone untrust: TCP SYN flood attack, occur
|
|
|
+ String regEx1 = "From\\s*(.+?):.+to\\s*(.+?):.+?threat\\s*name\\s*:\\s*(.+?),\\s*threat\\s*type\\s*:\\s*(.+?),\\s*threat\\s*subtype\\s*:\\s*(.+?),.+?severity\\s*:\\s*(.+?),";
|
|
|
+ String regEx2 = "alertlevel:(.+?)\\s\\sevent_type:(.+?)\\s.+?dst_ip:(.+?)\\s.+?src_ip:(.+?)\\s";
|
|
|
+ List<String> ips = new ArrayList<>();
|
|
|
+ Pattern p = Pattern.compile(regEx2);
|
|
|
+ Matcher m = p.matcher(content);
|
|
|
+ if (m.find()) {
|
|
|
+ System.out.println(m.group());
|
|
|
+ ips.add(m.group(1) + "," + m.group(2) + "," + m.group(3) + "," + m.group(4));
|
|
|
+ } else {
|
|
|
+ System.out.println("【未匹配】" + content);
|
|
|
+ }
|
|
|
+ return ips;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 限定符
|
|
|
+ * d? d可以出现0次或者一次 ? 0次或者1次
|
|
|
+ * ab*c a和c之间可以出现0个或者多个b * 0次或者多次
|
|
|
+ * ab+c a和c之间要出现1个以上的b + 一次或一次以上
|
|
|
+ * ab{6}c a和b之间要出现6个的b
|
|
|
+ * ab{2, 6}c a和b之间要出现2-6之间个的b
|
|
|
+ *
|
|
|
+ * 或
|
|
|
+ * a cat
|
|
|
+ * a dog
|
|
|
+ * a bird
|
|
|
+ *
|
|
|
+ * a (cat | dog) 可以匹配到上面的 a cat 和 a dog
|
|
|
+ *
|
|
|
+ * 字符类
|
|
|
+ *
|
|
|
+ * 元字符
|
|
|
+ * \d 代表数字字符
|
|
|
+ * \w 代表单词字符(也就是所有的英文字符、数字、下划线)
|
|
|
+ * \s 代表空白符(包含tab和换行符)
|
|
|
+ * \b 代表一个单词的开头或结尾
|
|
|
+ * \D 代表非数字字符
|
|
|
+ * \W 代表非单词字符
|
|
|
+ * \S 代表非空白字符
|
|
|
+ * . 代表任意字符(但不包含换行符)
|
|
|
+ * ^ 匹配行首 ^a 匹配以a开始的
|
|
|
+ * $ 匹配行尾 $a 匹配以a结尾的
|
|
|
+ *
|
|
|
+ * \b((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)\b
|
|
|
+ */
|
|
|
+ }
|
|
|
+}
|