@Test public void apply() throws Exception { long l = System.currentTimeMillis(); String url = "https: mockMvc.perform(post("/apply?name=小海博客Api测试，请忽略&url=" + url)).andDo(result -> { assertEquals(SUCCESS.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)); }); System.out.println("耗时：" + (System.currentTimeMillis() - l) / 1000 + "s"); url = "xxxxxxxxxm"; mockMvc.perform(post("/apply?name=小海博客Api测试，请忽略&url=" + url)).andDo(result -> { assertEquals(PARAMETERS_URL_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)); }); }
@PostMapping("/apply") public Response apply(@RequestParam("name") String name, @RequestParam("url") String url) { if (name == null || name.replaceAll(" ", "").isEmpty()) { return ResponseUtil.response(ResponseEnum.PARAMETERS_ERROR, null); } if (!RegexUtil.urlMatch(url)) { return ResponseUtil.response(ResponseEnum.PARAMETERS_URL_ERROR, null); } String applyTimeStr = redisUtil.get(request.getRemoteAddr() + "-Apply"); int applyTime = 0; if (applyTimeStr != null) { applyTime = Integer.parseInt(applyTimeStr); } if (applyTime == 10) { throw new MyException(ResponseEnum.FAILURE.getCode(), "申请次数已达10次，请2小时后重试"); } SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("友链申请：" + name); message.setTo("a@celess.cn"); message.setText("name:" + name + "\nurl:" + url + "\n" + DateFormatUtil.getNow()); Boolean send = mailService.send(message); redisUtil.setEx(request.getRemoteAddr() + "-Apply", applyTime + 1 + "", 2, TimeUnit.HOURS); return send ? ResponseUtil.success("") : ResponseUtil.failure(""); }
LinksController { @PostMapping("/apply") public Response apply(@RequestParam("name") String name, @RequestParam("url") String url) { if (name == null || name.replaceAll(" ", "").isEmpty()) { return ResponseUtil.response(ResponseEnum.PARAMETERS_ERROR, null); } if (!RegexUtil.urlMatch(url)) { return ResponseUtil.response(ResponseEnum.PARAMETERS_URL_ERROR, null); } String applyTimeStr = redisUtil.get(request.getRemoteAddr() + "-Apply"); int applyTime = 0; if (applyTimeStr != null) { applyTime = Integer.parseInt(applyTimeStr); } if (applyTime == 10) { throw new MyException(ResponseEnum.FAILURE.getCode(), "申请次数已达10次，请2小时后重试"); } SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("友链申请：" + name); message.setTo("a@celess.cn"); message.setText("name:" + name + "\nurl:" + url + "\n" + DateFormatUtil.getNow()); Boolean send = mailService.send(message); redisUtil.setEx(request.getRemoteAddr() + "-Apply", applyTime + 1 + "", 2, TimeUnit.HOURS); return send ? ResponseUtil.success("") : ResponseUtil.failure(""); } }
LinksController { @PostMapping("/apply") public Response apply(@RequestParam("name") String name, @RequestParam("url") String url) { if (name == null || name.replaceAll(" ", "").isEmpty()) { return ResponseUtil.response(ResponseEnum.PARAMETERS_ERROR, null); } if (!RegexUtil.urlMatch(url)) { return ResponseUtil.response(ResponseEnum.PARAMETERS_URL_ERROR, null); } String applyTimeStr = redisUtil.get(request.getRemoteAddr() + "-Apply"); int applyTime = 0; if (applyTimeStr != null) { applyTime = Integer.parseInt(applyTimeStr); } if (applyTime == 10) { throw new MyException(ResponseEnum.FAILURE.getCode(), "申请次数已达10次，请2小时后重试"); } SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("友链申请：" + name); message.setTo("a@celess.cn"); message.setText("name:" + name + "\nurl:" + url + "\n" + DateFormatUtil.getNow()); Boolean send = mailService.send(message); redisUtil.setEx(request.getRemoteAddr() + "-Apply", applyTime + 1 + "", 2, TimeUnit.HOURS); return send ? ResponseUtil.success("") : ResponseUtil.failure(""); }  }
LinksController { @PostMapping("/apply") public Response apply(@RequestParam("name") String name, @RequestParam("url") String url) { if (name == null || name.replaceAll(" ", "").isEmpty()) { return ResponseUtil.response(ResponseEnum.PARAMETERS_ERROR, null); } if (!RegexUtil.urlMatch(url)) { return ResponseUtil.response(ResponseEnum.PARAMETERS_URL_ERROR, null); } String applyTimeStr = redisUtil.get(request.getRemoteAddr() + "-Apply"); int applyTime = 0; if (applyTimeStr != null) { applyTime = Integer.parseInt(applyTimeStr); } if (applyTime == 10) { throw new MyException(ResponseEnum.FAILURE.getCode(), "申请次数已达10次，请2小时后重试"); } SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("友链申请：" + name); message.setTo("a@celess.cn"); message.setText("name:" + name + "\nurl:" + url + "\n" + DateFormatUtil.getNow()); Boolean send = mailService.send(message); redisUtil.setEx(request.getRemoteAddr() + "-Apply", applyTime + 1 + "", 2, TimeUnit.HOURS); return send ? ResponseUtil.success("") : ResponseUtil.failure(""); }  @PostMapping("/admin/links/create") Response create(@RequestBody LinkReq reqBody); @DeleteMapping("/admin/links/del/{id}") Response del(@PathVariable("id") long id); @PutMapping("/admin/links/update") Response update(@RequestBody LinkReq reqBody); @GetMapping("/links") Response allForOpen(); @GetMapping("/admin/links") Response all(@RequestParam("page") int page,
                        @RequestParam("count") int count); @PostMapping("/apply") Response apply(@RequestParam("name") String name,
                          @RequestParam("url") String url); }
LinksController { @PostMapping("/apply") public Response apply(@RequestParam("name") String name, @RequestParam("url") String url) { if (name == null || name.replaceAll(" ", "").isEmpty()) { return ResponseUtil.response(ResponseEnum.PARAMETERS_ERROR, null); } if (!RegexUtil.urlMatch(url)) { return ResponseUtil.response(ResponseEnum.PARAMETERS_URL_ERROR, null); } String applyTimeStr = redisUtil.get(request.getRemoteAddr() + "-Apply"); int applyTime = 0; if (applyTimeStr != null) { applyTime = Integer.parseInt(applyTimeStr); } if (applyTime == 10) { throw new MyException(ResponseEnum.FAILURE.getCode(), "申请次数已达10次，请2小时后重试"); } SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("友链申请：" + name); message.setTo("a@celess.cn"); message.setText("name:" + name + "\nurl:" + url + "\n" + DateFormatUtil.getNow()); Boolean send = mailService.send(message); redisUtil.setEx(request.getRemoteAddr() + "-Apply", applyTime + 1 + "", 2, TimeUnit.HOURS); return send ? ResponseUtil.success("") : ResponseUtil.failure(""); }  @PostMapping("/admin/links/create") Response create(@RequestBody LinkReq reqBody); @DeleteMapping("/admin/links/del/{id}") Response del(@PathVariable("id") long id); @PutMapping("/admin/links/update") Response update(@RequestBody LinkReq reqBody); @GetMapping("/links") Response allForOpen(); @GetMapping("/admin/links") Response all(@RequestParam("page") int page,
                        @RequestParam("count") int count); @PostMapping("/apply") Response apply(@RequestParam("name") String name,
                          @RequestParam("url") String url);  }