From 5cb7ee80b8ad614764f043736c6c0d7046a566af Mon Sep 17 00:00:00 2001 From: Jesse-Ma <24167796@qq.com> Date: Fri, 9 Dec 2022 14:16:15 +0800 Subject: [PATCH] inittime --- pom.xml | 8 +++- .../note/controller/NoteController.java | 6 ++- .../java/com/flagnote/note/entity/Note.java | 2 + .../note/repository/NoteMongoRepository.java | 9 ++++ .../flagnote/note/schedule/NoteSchedule.java | 45 +++++++++++++++++++ .../note/service/NoteServiceImpl.java | 2 +- .../com/flagnote/note/utils/BizKeyUtils.java | 43 +++++++++++++++++- src/main/resources/application.yml | 21 ++++++++- 8 files changed, 127 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/flagnote/note/schedule/NoteSchedule.java diff --git a/pom.xml b/pom.xml index 9e8c3bc..141a1c6 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.7.2 + 2.7.6 com.flagnote @@ -25,6 +25,10 @@ org.springframework.boot spring-boot-starter-data-mongodb + + org.springframework.boot + spring-boot-starter-quartz + org.springframework.boot spring-boot-starter-web @@ -32,7 +36,7 @@ cn.hutool hutool-all - 5.7.22 + 5.8.10 org.projectlombok diff --git a/src/main/java/com/flagnote/note/controller/NoteController.java b/src/main/java/com/flagnote/note/controller/NoteController.java index f40689f..24018f6 100644 --- a/src/main/java/com/flagnote/note/controller/NoteController.java +++ b/src/main/java/com/flagnote/note/controller/NoteController.java @@ -40,8 +40,9 @@ public class NoteController { KeyMeta c = new KeyMeta(); String key = BizKeyUtils.getKey(); c.setKey(key); - c.setCipher(BizKeyUtils.getCipher(key)); c.setServerTime(new Date().getTime()); + c.setCipher(BizKeyUtils.getCipher(key,c.getServerTime().toString())); + return c; } @@ -105,7 +106,7 @@ 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, - @RequestParam("lock") Integer lock, @RequestParam("md5") String md5) throws IOException { + @RequestParam("lock") Integer lock, @RequestParam("md5") String md5,@RequestParam("initTime") String initTime) throws IOException { Date pushTime = new Date(); Note note = new Note(); @@ -113,6 +114,7 @@ public class NoteController { note.setTextBytes(file.getBytes()); note.setLock(lock); note.setMd5(md5); + note.setInitTime(new Date(Long.parseLong(initTime))); note.setPushTime(pushTime); noteService.saveNote(note); diff --git a/src/main/java/com/flagnote/note/entity/Note.java b/src/main/java/com/flagnote/note/entity/Note.java index e3c9004..09ab5a3 100644 --- a/src/main/java/com/flagnote/note/entity/Note.java +++ b/src/main/java/com/flagnote/note/entity/Note.java @@ -24,6 +24,8 @@ public class Note implements Serializable { private String cipher; private Integer state; + + private Date initTime; private Date pushTime; diff --git a/src/main/java/com/flagnote/note/repository/NoteMongoRepository.java b/src/main/java/com/flagnote/note/repository/NoteMongoRepository.java index 507cbc4..29b08e3 100644 --- a/src/main/java/com/flagnote/note/repository/NoteMongoRepository.java +++ b/src/main/java/com/flagnote/note/repository/NoteMongoRepository.java @@ -1,11 +1,20 @@ package com.flagnote.note.repository; +import java.util.Date; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; import org.springframework.stereotype.Repository; import com.flagnote.note.entity.Note; @Repository public interface NoteMongoRepository extends MongoRepository { + + + @Query(value = "{'state' : {'$eq' : 1},'expireTime' : {'$lt' : ?0}}") + public Page findExpireNote(Date date,Pageable page); } diff --git a/src/main/java/com/flagnote/note/schedule/NoteSchedule.java b/src/main/java/com/flagnote/note/schedule/NoteSchedule.java new file mode 100644 index 0000000..4be2755 --- /dev/null +++ b/src/main/java/com/flagnote/note/schedule/NoteSchedule.java @@ -0,0 +1,45 @@ +package com.flagnote.note.schedule; + +import java.util.Date; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.domain.Example; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; + +import com.flagnote.note.entity.Note; +import com.flagnote.note.repository.NoteMongoRepository; + +@EnableScheduling +@Configuration +public class NoteSchedule { + + @Autowired + private NoteMongoRepository noteMongoRepository; + + @Scheduled(cron = "0/10 * * * * ?") + public void updateExpiredNote() { + + while(true) { + + PageRequest pageRequest = PageRequest.of(0,5); + Page noteList = noteMongoRepository.findExpireNote(new Date(),pageRequest); + + if(noteList.getNumberOfElements()>0) { + for (Note note : noteList) { + note.setState(0); + note.setTextBytes(null); + } + noteMongoRepository.saveAll(noteList); + }else { + break; + } + } + + } +} diff --git a/src/main/java/com/flagnote/note/service/NoteServiceImpl.java b/src/main/java/com/flagnote/note/service/NoteServiceImpl.java index 624e280..b8e625f 100644 --- a/src/main/java/com/flagnote/note/service/NoteServiceImpl.java +++ b/src/main/java/com/flagnote/note/service/NoteServiceImpl.java @@ -53,7 +53,7 @@ public class NoteServiceImpl implements NoteService { note.setExpireTime(DateUtil.offsetHour(note.getPushTime(), 1)); - note.setRetentionTime(DateUtil.offsetSecond(note.getPushTime(), 4000 * 3600)); + note.setRetentionTime(DateUtil.offsetSecond(note.getPushTime(), 400 * 3600)); note.setState(1); diff --git a/src/main/java/com/flagnote/note/utils/BizKeyUtils.java b/src/main/java/com/flagnote/note/utils/BizKeyUtils.java index 54ecb43..a6fb3b6 100644 --- a/src/main/java/com/flagnote/note/utils/BizKeyUtils.java +++ b/src/main/java/com/flagnote/note/utils/BizKeyUtils.java @@ -84,12 +84,51 @@ public class BizKeyUtils { return Integer.parseInt(String.valueOf(new Date().getTime()).substring(0, 4)); } - public static String getCipher(String key) { - return md5(key + MIX_STRING + key); + public static String getCipher(String key,String initTime) { + return md5(key +"#"+ MIX_STRING + "#" + initTime); } + + public static Boolean validateCipher(String key,String initTime,String cipher) { + return md5(key +"#"+ MIX_STRING + "#" + initTime).equals(cipher); + } + public static String getSecretKey(String key, String password) { return md5(key + md5(MIX_STRING + password)); + } + + public static void main(String[] args) { + + int c = 0; + while(true) { + c++; + StringBuffer sb = new StringBuffer(); + + for (int i = 0; i < 16; i++) { + sb.append(RANGE_STRING.charAt(RandomUtils.nextInt(30))); + } + + Boolean result = validateKey(sb.toString()); + + if(c%1000==0) { + System.out.println(c); + System.out.println(result); + System.out.println(sb.toString()); + } + + if(result) { + System.out.println(c); + System.out.println(result); + System.out.println(sb.toString()); + break; + } + + } + + + + + } } \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 08013a8..c7bfdf1 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,5 +1,6 @@ server: - port: 3333 + address: 127.0.0.1 + port: 20000 spring: codec: max-in-memory-size: 128MB @@ -13,4 +14,20 @@ spring: password: flagnote1qazx autoConnectRetry: true socketKeepAlive: true - socketTimeout: 1000 \ No newline at end of file + socketTimeout: 1000 + +management: +# server: +# address: 127.0.0.1 +# port: 29000 + endpoint: + shutdown: + enabled: true + health: + enabled: true + endpoints: + web: + base-path: /f2w8u47ie56edc93/actuator + exposure: + include: shutdown,health +