getSecretKey jsonResult getNoteTxt

This commit is contained in:
Jesse-Ma
2023-03-01 17:10:16 +08:00
parent aa3315a909
commit 3f197dde05
7 changed files with 114 additions and 42 deletions

View File

@@ -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,34 +35,38 @@ 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());
meta.setMd5(note.getMd5());
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)

View File

@@ -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;

View File

@@ -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;

View File

@@ -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()));
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,10 +97,10 @@ 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;

View File

@@ -10,6 +10,8 @@ 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,14 +96,9 @@ public class BizKeyUtils {
}
public static String getSecretKey(String key, String password) {
if (!StringUtils.hasText(password)) {
password = key;
public static String getSecretKey(String key) {
return md5(key +"#"+SECRET_KEY_STRING);
}
String aesString = SecretUtils.aesEncode(password, key);
return md5(aesString+"_F1agn0te");
}
public static void main(String[] args) {

View File

@@ -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;
}
}

View File

@@ -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;
}