diff --git a/src/main/java/com/flagnote/note/controller/NoteController.java b/src/main/java/com/flagnote/note/controller/NoteController.java index 46f160e..cf8da1e 100644 --- a/src/main/java/com/flagnote/note/controller/NoteController.java +++ b/src/main/java/com/flagnote/note/controller/NoteController.java @@ -20,6 +20,7 @@ import com.flagnote.note.entity.Note; import com.flagnote.note.entity.NoteMeta; import com.flagnote.note.service.NoteService; import com.flagnote.note.utils.BizKeyUtils; +import com.flagnote.note.utils.JsonResult; import cn.hutool.core.util.ZipUtil; import jakarta.servlet.http.HttpServletResponse; @@ -34,26 +35,26 @@ public class NoteController { private NoteService noteService; @RequestMapping(value = "/note/keyMeta", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) - public KeyMeta getKeyMeta() { + public JsonResult getKeyMeta() { KeyMeta c = new KeyMeta(); String key = BizKeyUtils.getKey(); c.setKey(key); c.setServerTime(new Date().getTime()); c.setCipher(BizKeyUtils.getCipher(key,c.getServerTime().toString())); - - return c; + c.setSecretKey(BizKeyUtils.getSecretKey(key)); + return JsonResult.ok(c); } @RequestMapping(value = "/note/{key:[abcdefghijkmnopqrstuvwxyz23456789]{16}}/noteMeta", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) - public NoteMeta getNoteMeta(@PathVariable("key") String key) { + public JsonResult getNoteMeta(@PathVariable("key") String key) { NoteMeta meta = new NoteMeta(); Note note = noteService.getNote(key); meta.setKey(key); + meta.setSecretKey(BizKeyUtils.getSecretKey(key)); if (null == note) { - return meta; + return JsonResult.ok(meta); } - meta.setServerTime(new Date().getTime()); meta.setLock(note.getLock()); @@ -61,7 +62,11 @@ public class NoteController { meta.setState(note.getState()); meta.setTtl(note.getTtl()); - return meta; + if(meta.getState()!=1) { + meta.setSecretKey(null); + } + + return JsonResult.ok(meta); } @RequestMapping(value = "/note/{key:[abcdefghijkmnopqrstuvwxyz23456789]{16}}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) @@ -102,8 +107,8 @@ public class NoteController { } - @RequestMapping(value = "/note/{key:[abcdefghijkmnopqrstuvwxyz23456789]{16}}", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public String saveNote(@PathVariable("key") String key, @RequestPart("file") MultipartFile file, + @RequestMapping(value = "/note/{key:[abcdefghijkmnopqrstuvwxyz23456789]{16}}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public JsonResult saveNote(@PathVariable("key") String key, @RequestPart("file") MultipartFile file, @RequestParam("lock") Integer lock, @RequestParam("md5") String md5,@RequestParam("initTime") String initTime) throws IOException { Date pushTime = new Date(); @@ -117,13 +122,13 @@ public class NoteController { noteService.saveNote(note); - return key; + return JsonResult.ok(key); } - @RequestMapping(value = "/note/{key:[abcdefghijkmnopqrstuvwxyz23456789]{16}}/delete", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) - public String deleteNote(@PathVariable("key") String key) { + @RequestMapping(value = "/note/{key:[abcdefghijkmnopqrstuvwxyz23456789]{16}}/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) + public JsonResult deleteNote(@PathVariable("key") String key) { noteService.deleteNote(key); - return key; + return JsonResult.ok(key); } @RequestMapping(value = "/note/{key:[abcdefghijkmnopqrstuvwxyz23456789]{16}}.txt", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE) diff --git a/src/main/java/com/flagnote/note/entity/KeyMeta.java b/src/main/java/com/flagnote/note/entity/KeyMeta.java index ba9892a..01d192b 100644 --- a/src/main/java/com/flagnote/note/entity/KeyMeta.java +++ b/src/main/java/com/flagnote/note/entity/KeyMeta.java @@ -1,18 +1,18 @@ package com.flagnote.note.entity; import java.io.Serializable; -import java.util.Date; import lombok.Data; @Data -public class KeyMeta implements Serializable {/** - * - */ +public class KeyMeta implements Serializable { + private static final long serialVersionUID = -2816061962747625732L; private String key; + private String secretKey; + private String cipher; private Long serverTime; diff --git a/src/main/java/com/flagnote/note/entity/NoteMeta.java b/src/main/java/com/flagnote/note/entity/NoteMeta.java index ca5d0aa..78ede53 100644 --- a/src/main/java/com/flagnote/note/entity/NoteMeta.java +++ b/src/main/java/com/flagnote/note/entity/NoteMeta.java @@ -5,17 +5,14 @@ import java.io.Serializable; import lombok.Data; @Data -public class NoteMeta implements Serializable {/** - * - */ +public class NoteMeta implements Serializable { + private static final long serialVersionUID = -8234044213813670440L; -/** - * - */ - private String key; + private String secretKey; + private Integer state; private String cipher; diff --git a/src/main/java/com/flagnote/note/service/NoteServiceImpl.java b/src/main/java/com/flagnote/note/service/NoteServiceImpl.java index 0219b47..98ec6b8 100644 --- a/src/main/java/com/flagnote/note/service/NoteServiceImpl.java +++ b/src/main/java/com/flagnote/note/service/NoteServiceImpl.java @@ -7,17 +7,15 @@ import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.util.Assert; import com.flagnote.note.entity.Note; import com.flagnote.note.repository.NoteMongoRepository; import com.flagnote.note.utils.BizKeyUtils; import com.flagnote.note.utils.SecretUtils; -import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ArrayUtil; -import cn.hutool.core.util.ByteUtil; -import cn.hutool.core.util.CharUtil; import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.ZipUtil; @@ -76,7 +74,15 @@ public class NoteServiceImpl implements NoteService { note.setTextBytes(SecretUtils.encodeNote(note.getTextBytes())); - noteMongoRepository.save(note); + Note exists = noteMongoRepository.findById(mixKey).orElse(null); + + if (null == exists) { + noteMongoRepository.save(note); + } else { + if (!exists.getMd5().equals(note.getMd5())) { + throw new RuntimeException("E:800001"); + } + } } @Override @@ -91,16 +97,16 @@ public class NoteServiceImpl implements NoteService { } private String desNote(String key, Note note) { - if(null==note.getTextBytes()) { + if (null == note.getTextBytes()) { return null; } - String secretKey = BizKeyUtils.getSecretKey(key, ""); - + String secretKey = BizKeyUtils.getSecretKey(key); + byte[] asebts = ZipUtil.unGzip(note.getTextBytes()); Charset cs = CharsetUtil.CHARSET_UTF_8; - ByteBuffer bb = ByteBuffer.allocate(asebts.length); - bb.put(asebts).flip(); - CharBuffer cb = cs.decode(bb); + ByteBuffer bb = ByteBuffer.allocate(asebts.length); + bb.put(asebts).flip(); + CharBuffer cb = cs.decode(bb); String result = SecretUtils.aesDecode(new String(cb.array()), secretKey); if (result.startsWith("FLAGNOTE#")) { return result.substring(9); diff --git a/src/main/java/com/flagnote/note/utils/BizKeyUtils.java b/src/main/java/com/flagnote/note/utils/BizKeyUtils.java index 5ebad4f..d0def81 100644 --- a/src/main/java/com/flagnote/note/utils/BizKeyUtils.java +++ b/src/main/java/com/flagnote/note/utils/BizKeyUtils.java @@ -9,7 +9,9 @@ import org.springframework.util.StringUtils; public class BizKeyUtils { public static final String MIX_STRING = "6v8muhqp8ta45ncsyi8y"; - + + public static final String SECRET_KEY_STRING = "dswoun5atk2q363fmdfe"; + public static final String RANGE_STRING = "abcdefhikmnopqstuvwxyz23456789"; public static String getKey() { @@ -94,15 +96,10 @@ public class BizKeyUtils { } - public static String getSecretKey(String key, String password) { - if (!StringUtils.hasText(password)) { - password = key; - } - String aesString = SecretUtils.aesEncode(password, key); - return md5(aesString+"_F1agn0te"); + public static String getSecretKey(String key) { + return md5(key +"#"+SECRET_KEY_STRING); } - public static void main(String[] args) { int c = 0; diff --git a/src/main/java/com/flagnote/note/utils/GlobalExceptionHandler.java b/src/main/java/com/flagnote/note/utils/GlobalExceptionHandler.java new file mode 100644 index 0000000..8a548e7 --- /dev/null +++ b/src/main/java/com/flagnote/note/utils/GlobalExceptionHandler.java @@ -0,0 +1,39 @@ +package com.flagnote.note.utils; + +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(RuntimeException.class) + public Object businessExceptionHandler(HttpServletRequest request,HttpServletResponse response,Exception e) + { + log.error("ExceptionHandler(RuntimeException.class)",e); + JsonResult jsonResult = new JsonResult(); + String message = e.getMessage(); + if (null!=message && message.matches("^E:\\d{6}$")) { + jsonResult.setCode(message.substring(2, 8)); + }else { + jsonResult.setCode("800000"); + } + jsonResult.setMessage("business exception"); + return jsonResult; + } + + @ExceptionHandler(Exception.class) + public Object exceptionHandler(HttpServletRequest request,HttpServletResponse response,Exception e) + { + log.error("ExceptionHandler(Exception.class)",e); + JsonResult jsonResult = new JsonResult(); + jsonResult.setCode("900000"); + jsonResult.setMessage("exception"); + return jsonResult; + } + +} diff --git a/src/main/java/com/flagnote/note/utils/JsonResult.java b/src/main/java/com/flagnote/note/utils/JsonResult.java new file mode 100644 index 0000000..e8309bd --- /dev/null +++ b/src/main/java/com/flagnote/note/utils/JsonResult.java @@ -0,0 +1,28 @@ +package com.flagnote.note.utils; + +import java.io.Serializable; + +import lombok.Data; + +@Data +public class JsonResult implements Serializable { + /** + * + */ + private static final long serialVersionUID = -6842328051422997473L; + + public static JsonResult ok(Object obj) { + JsonResult res = new JsonResult(); + res.setCode("000000"); + res.setMessage("ok"); + res.setData(obj); + return res; + } + + private String code; + + private String message; + + private Object data; + +}