getNoteTxt

This commit is contained in:
Jesse-Ma
2023-02-27 09:53:06 +08:00
parent 4a17a49fdf
commit e5b97d1e5d
6 changed files with 92 additions and 8 deletions

View File

@@ -5,7 +5,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version> <version>3.0.2</version>
<relativePath /> <!-- lookup parent from repository --> <relativePath /> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>com.flagnote</groupId> <groupId>com.flagnote</groupId>
@@ -15,7 +15,7 @@
<description>Demo project for Spring Boot</description> <description>Demo project for Spring Boot</description>
<properties> <properties>
<java.version>17</java.version> <java.version>17</java.version>
<spring-cloud.version>2022.0.0</spring-cloud.version> <spring-cloud.version>2022.0.1</spring-cloud.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>

View File

@@ -21,6 +21,7 @@ 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 cn.hutool.core.util.ZipUtil;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -125,4 +126,10 @@ public class NoteController {
return key; return key;
} }
@RequestMapping(value = "/note/{key:[abcdefghijkmnopqrstuvwxyz23456789]{16}}.txt", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
public String getNoteText(@PathVariable("key") String key) {
return noteService.getNoteText(key);
}
} }

View File

@@ -10,4 +10,6 @@ public interface NoteService {
void deleteNote(String key); void deleteNote(String key);
String getNoteText(String key);
} }

View File

@@ -1,5 +1,8 @@
package com.flagnote.note.service; package com.flagnote.note.service;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Date; import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -10,8 +13,13 @@ 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.ZipUtil;
@Service @Service
public class NoteServiceImpl implements NoteService { public class NoteServiceImpl implements NoteService {
@@ -45,6 +53,15 @@ public class NoteServiceImpl implements NoteService {
return note; return note;
} }
@Override
public String getNoteText(String key) {
Note note = this.getNote(key);
if (null == note || ArrayUtil.isEmpty(note.getTextBytes())) {
return null;
}
return desNote(key, note);
}
@Override @Override
public void saveNote(Note note) { public void saveNote(Note note) {
String mixKey = BizKeyUtils.mixKey(note.getKey()); String mixKey = BizKeyUtils.mixKey(note.getKey());
@@ -66,11 +83,29 @@ public class NoteServiceImpl implements NoteService {
public void deleteNote(String key) { public void deleteNote(String key) {
String mixKey = BizKeyUtils.mixKey(key); String mixKey = BizKeyUtils.mixKey(key);
Note note = noteMongoRepository.findById(mixKey).orElse(null); Note note = noteMongoRepository.findById(mixKey).orElse(null);
if (null != note && ArrayUtil.isNotEmpty(note.getTextBytes())) { if (null != note && ArrayUtil.isNotEmpty(note.getTextBytes())) {
note.setState(0); note.setState(0);
note.setTextBytes(null); note.setTextBytes(null);
noteMongoRepository.save(note); noteMongoRepository.save(note);
} }
} }
private String desNote(String key, Note note) {
if(null==note.getTextBytes()) {
return null;
}
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);
String result = SecretUtils.aesDecode(new String(cb.array()), secretKey);
if (result.startsWith("FLAGNOTE#")) {
return result.substring(9);
}
return "";
}
} }

View File

@@ -4,6 +4,7 @@ import java.io.UnsupportedEncodingException;
import java.util.Date; import java.util.Date;
import org.springframework.util.DigestUtils; import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
public class BizKeyUtils { public class BizKeyUtils {
@@ -94,8 +95,13 @@ public class BizKeyUtils {
public static String getSecretKey(String key, String password) { public static String getSecretKey(String key, String password) {
return md5(key + md5(MIX_STRING + password)); if (!StringUtils.hasText(password)) {
password = key;
} }
String aesString = SecretUtils.aesEncode(password, key);
return md5(aesString+"_F1agn0te");
}
public static void main(String[] args) { public static void main(String[] args) {

View File

@@ -1,21 +1,55 @@
package com.flagnote.note.utils; package com.flagnote.note.utils;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.Mode; import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding; import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES; import cn.hutool.crypto.symmetric.AES;
public class SecretUtils { public class SecretUtils {
private static AES aes = new AES(Mode.ECB, Padding.PKCS5Padding, private static AES aesNote = new AES(Mode.ECB, Padding.PKCS5Padding,
new SecretKeySpec("b9t3pzbmybc66cba".getBytes(), "AES")); new SecretKeySpec("b9t3pzbmybc66cba".getBytes(), "AES"));
public static byte[] encodeNote(byte[] text) { public static byte[] encodeNote(byte[] text) {
return aes.encrypt(text); return aesNote.encrypt(text);
} }
public static byte[] decodeNote(byte[] text) { public static byte[] decodeNote(byte[] text) {
return aes.decrypt(text); return aesNote.decrypt(text);
} }
public static String aesEncode(String word,String keyWord) {
AES aes = new AES(Mode.ECB, Padding.PKCS5Padding,
new SecretKeySpec(keyWord.getBytes(), "AES"));
return aes.encryptHex(word);
}
public static String aesDecode(String word,String keyWord) {
AES aes = new AES(Mode.ECB, Padding.PKCS5Padding,
new SecretKeySpec(keyWord.getBytes(), "AES"));
return new String(aes.decrypt(HexUtil.decodeHex(word)));
}
public static void main(String[] args) {
System.out.println(aesEncode("kaxfn73w2492eb2a","kaxfn73w2492eb2a"));;
AES aesNote = new AES(Mode.ECB, Padding.PKCS5Padding,
new SecretKeySpec("kaxfn73w2492eb2a".getBytes(), "AES"));
System.out.println(aesNote.encryptBase64("kaxfn73w2492eb2a"));
System.out.println(new String(aesNote.decrypt(Base64.decode("ycaXaJnQZz9sq/3ezdEB66s4rZv9POL4Q9HRIOWtQPo="))));
}
} }