getSecretKey jsonResult getNoteTxt
This commit is contained in:
@@ -20,6 +20,7 @@ import com.flagnote.note.entity.Note;
|
|||||||
import com.flagnote.note.entity.NoteMeta;
|
import com.flagnote.note.entity.NoteMeta;
|
||||||
import com.flagnote.note.service.NoteService;
|
import com.flagnote.note.service.NoteService;
|
||||||
import com.flagnote.note.utils.BizKeyUtils;
|
import com.flagnote.note.utils.BizKeyUtils;
|
||||||
|
import com.flagnote.note.utils.JsonResult;
|
||||||
|
|
||||||
import cn.hutool.core.util.ZipUtil;
|
import cn.hutool.core.util.ZipUtil;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
@@ -34,26 +35,26 @@ public class NoteController {
|
|||||||
private NoteService noteService;
|
private NoteService noteService;
|
||||||
|
|
||||||
@RequestMapping(value = "/note/keyMeta", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
@RequestMapping(value = "/note/keyMeta", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||||
public KeyMeta getKeyMeta() {
|
public JsonResult getKeyMeta() {
|
||||||
KeyMeta c = new KeyMeta();
|
KeyMeta c = new KeyMeta();
|
||||||
String key = BizKeyUtils.getKey();
|
String key = BizKeyUtils.getKey();
|
||||||
c.setKey(key);
|
c.setKey(key);
|
||||||
c.setServerTime(new Date().getTime());
|
c.setServerTime(new Date().getTime());
|
||||||
c.setCipher(BizKeyUtils.getCipher(key,c.getServerTime().toString()));
|
c.setCipher(BizKeyUtils.getCipher(key,c.getServerTime().toString()));
|
||||||
|
c.setSecretKey(BizKeyUtils.getSecretKey(key));
|
||||||
return c;
|
return JsonResult.ok(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/note/{key:[abcdefghijkmnopqrstuvwxyz23456789]{16}}/noteMeta", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
@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();
|
NoteMeta meta = new NoteMeta();
|
||||||
Note note = noteService.getNote(key);
|
Note note = noteService.getNote(key);
|
||||||
meta.setKey(key);
|
meta.setKey(key);
|
||||||
|
meta.setSecretKey(BizKeyUtils.getSecretKey(key));
|
||||||
|
|
||||||
if (null == note) {
|
if (null == note) {
|
||||||
return meta;
|
return JsonResult.ok(meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
meta.setServerTime(new Date().getTime());
|
meta.setServerTime(new Date().getTime());
|
||||||
meta.setLock(note.getLock());
|
meta.setLock(note.getLock());
|
||||||
@@ -61,7 +62,11 @@ public class NoteController {
|
|||||||
meta.setState(note.getState());
|
meta.setState(note.getState());
|
||||||
meta.setTtl(note.getTtl());
|
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)
|
@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)
|
@RequestMapping(value = "/note/{key:[abcdefghijkmnopqrstuvwxyz23456789]{16}}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
public String saveNote(@PathVariable("key") String key, @RequestPart("file") MultipartFile file,
|
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 {
|
@RequestParam("lock") Integer lock, @RequestParam("md5") String md5,@RequestParam("initTime") String initTime) throws IOException {
|
||||||
Date pushTime = new Date();
|
Date pushTime = new Date();
|
||||||
|
|
||||||
@@ -117,13 +122,13 @@ public class NoteController {
|
|||||||
|
|
||||||
noteService.saveNote(note);
|
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)
|
@RequestMapping(value = "/note/{key:[abcdefghijkmnopqrstuvwxyz23456789]{16}}/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||||
public String deleteNote(@PathVariable("key") String key) {
|
public JsonResult deleteNote(@PathVariable("key") String key) {
|
||||||
noteService.deleteNote(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)
|
@RequestMapping(value = "/note/{key:[abcdefghijkmnopqrstuvwxyz23456789]{16}}.txt", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
package com.flagnote.note.entity;
|
package com.flagnote.note.entity;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class KeyMeta implements Serializable {/**
|
public class KeyMeta implements Serializable {
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static final long serialVersionUID = -2816061962747625732L;
|
private static final long serialVersionUID = -2816061962747625732L;
|
||||||
|
|
||||||
private String key;
|
private String key;
|
||||||
|
|
||||||
|
private String secretKey;
|
||||||
|
|
||||||
private String cipher;
|
private String cipher;
|
||||||
|
|
||||||
private Long serverTime;
|
private Long serverTime;
|
||||||
|
|||||||
@@ -5,17 +5,14 @@ import java.io.Serializable;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class NoteMeta implements Serializable {/**
|
public class NoteMeta implements Serializable {
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static final long serialVersionUID = -8234044213813670440L;
|
private static final long serialVersionUID = -8234044213813670440L;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
private String key;
|
private String key;
|
||||||
|
|
||||||
|
private String secretKey;
|
||||||
|
|
||||||
private Integer state;
|
private Integer state;
|
||||||
|
|
||||||
private String cipher;
|
private String cipher;
|
||||||
|
|||||||
@@ -7,17 +7,15 @@ import java.util.Date;
|
|||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
import com.flagnote.note.entity.Note;
|
import com.flagnote.note.entity.Note;
|
||||||
import com.flagnote.note.repository.NoteMongoRepository;
|
import com.flagnote.note.repository.NoteMongoRepository;
|
||||||
import com.flagnote.note.utils.BizKeyUtils;
|
import com.flagnote.note.utils.BizKeyUtils;
|
||||||
import com.flagnote.note.utils.SecretUtils;
|
import com.flagnote.note.utils.SecretUtils;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
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.CharsetUtil;
|
||||||
import cn.hutool.core.util.ZipUtil;
|
import cn.hutool.core.util.ZipUtil;
|
||||||
|
|
||||||
@@ -76,7 +74,15 @@ public class NoteServiceImpl implements NoteService {
|
|||||||
|
|
||||||
note.setTextBytes(SecretUtils.encodeNote(note.getTextBytes()));
|
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
|
@Override
|
||||||
@@ -91,16 +97,16 @@ public class NoteServiceImpl implements NoteService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String desNote(String key, Note note) {
|
private String desNote(String key, Note note) {
|
||||||
if(null==note.getTextBytes()) {
|
if (null == note.getTextBytes()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String secretKey = BizKeyUtils.getSecretKey(key, "");
|
String secretKey = BizKeyUtils.getSecretKey(key);
|
||||||
|
|
||||||
byte[] asebts = ZipUtil.unGzip(note.getTextBytes());
|
byte[] asebts = ZipUtil.unGzip(note.getTextBytes());
|
||||||
Charset cs = CharsetUtil.CHARSET_UTF_8;
|
Charset cs = CharsetUtil.CHARSET_UTF_8;
|
||||||
ByteBuffer bb = ByteBuffer.allocate(asebts.length);
|
ByteBuffer bb = ByteBuffer.allocate(asebts.length);
|
||||||
bb.put(asebts).flip();
|
bb.put(asebts).flip();
|
||||||
CharBuffer cb = cs.decode(bb);
|
CharBuffer cb = cs.decode(bb);
|
||||||
String result = SecretUtils.aesDecode(new String(cb.array()), secretKey);
|
String result = SecretUtils.aesDecode(new String(cb.array()), secretKey);
|
||||||
if (result.startsWith("FLAGNOTE#")) {
|
if (result.startsWith("FLAGNOTE#")) {
|
||||||
return result.substring(9);
|
return result.substring(9);
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ import org.springframework.util.StringUtils;
|
|||||||
public class BizKeyUtils {
|
public class BizKeyUtils {
|
||||||
|
|
||||||
public static final String MIX_STRING = "6v8muhqp8ta45ncsyi8y";
|
public static final String MIX_STRING = "6v8muhqp8ta45ncsyi8y";
|
||||||
|
|
||||||
|
public static final String SECRET_KEY_STRING = "dswoun5atk2q363fmdfe";
|
||||||
|
|
||||||
public static final String RANGE_STRING = "abcdefhikmnopqstuvwxyz23456789";
|
public static final String RANGE_STRING = "abcdefhikmnopqstuvwxyz23456789";
|
||||||
|
|
||||||
public static String getKey() {
|
public static String getKey() {
|
||||||
@@ -94,15 +96,10 @@ public class BizKeyUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String getSecretKey(String key, String password) {
|
public static String getSecretKey(String key) {
|
||||||
if (!StringUtils.hasText(password)) {
|
return md5(key +"#"+SECRET_KEY_STRING);
|
||||||
password = key;
|
|
||||||
}
|
|
||||||
String aesString = SecretUtils.aesEncode(password, key);
|
|
||||||
return md5(aesString+"_F1agn0te");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
int c = 0;
|
int c = 0;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
28
src/main/java/com/flagnote/note/utils/JsonResult.java
Normal file
28
src/main/java/com/flagnote/note/utils/JsonResult.java
Normal 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;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user