From 8effadb8822cc31b263340a2ced2ce16a3092e28 Mon Sep 17 00:00:00 2001 From: abel Date: Tue, 23 Jun 2020 22:40:48 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1/=EC=88=98=EC=A0=95=EC=8B=9C=20=EA=B8=88=EC=B9=99?= =?UTF-8?q?=EC=96=B4=20=EC=B2=B4=ED=81=AC=20=EB=A1=9C=EC=A7=81=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/rest/api/advice/ExceptionAdvice.java | 6 ++++++ .../exception/CForbiddenWordException.java | 16 ++++++++++++++++ .../com/rest/api/service/board/BoardService.java | 14 ++++++++++++++ src/main/resources/i18n/exception_en.yml | 5 ++++- src/main/resources/i18n/exception_ko.yml | 5 ++++- 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/rest/api/advice/exception/CForbiddenWordException.java diff --git a/src/main/java/com/rest/api/advice/ExceptionAdvice.java b/src/main/java/com/rest/api/advice/ExceptionAdvice.java index 3ff7860..950967d 100644 --- a/src/main/java/com/rest/api/advice/ExceptionAdvice.java +++ b/src/main/java/com/rest/api/advice/ExceptionAdvice.java @@ -77,6 +77,12 @@ public class ExceptionAdvice { return responseService.getFailResult(Integer.valueOf(getMessage("resourceNotExist.code")), getMessage("resourceNotExist.msg")); } + @ExceptionHandler(CForbiddenWordException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public CommonResult forbiddenWordException(HttpServletRequest request, CForbiddenWordException e) { + return responseService.getFailResult(Integer.valueOf(getMessage("forbiddenWord.code")), getMessage("forbiddenWord.msg", new Object[]{e.getMessage()})); + } + // code정보에 해당하는 메시지를 조회합니다. private String getMessage(String code) { return getMessage(code, null); diff --git a/src/main/java/com/rest/api/advice/exception/CForbiddenWordException.java b/src/main/java/com/rest/api/advice/exception/CForbiddenWordException.java new file mode 100644 index 0000000..8c3cdeb --- /dev/null +++ b/src/main/java/com/rest/api/advice/exception/CForbiddenWordException.java @@ -0,0 +1,16 @@ +package com.rest.api.advice.exception; + +public class CForbiddenWordException extends RuntimeException { + + public CForbiddenWordException(String msg, Throwable t) { + super(msg, t); + } + + public CForbiddenWordException(String msg) { + super(msg); + } + + public CForbiddenWordException() { + super(); + } +} diff --git a/src/main/java/com/rest/api/service/board/BoardService.java b/src/main/java/com/rest/api/service/board/BoardService.java index 0228ed9..4a44200 100644 --- a/src/main/java/com/rest/api/service/board/BoardService.java +++ b/src/main/java/com/rest/api/service/board/BoardService.java @@ -1,5 +1,6 @@ package com.rest.api.service.board; +import com.rest.api.advice.exception.CForbiddenWordException; import com.rest.api.advice.exception.CNotOwnerException; import com.rest.api.advice.exception.CResourceNotExistException; import com.rest.api.advice.exception.CUserNotFoundException; @@ -20,6 +21,7 @@ import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import javax.transaction.Transactional; +import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -60,6 +62,8 @@ public class BoardService { @CacheEvict(value = CacheKey.POSTS, key = "#boardName") public Post writePost(String uid, String boardName, ParamsPost paramsPost) { Board board = findBoard(boardName); + // 금칙어 체크 + checkForbiddenWord(paramsPost.getContent()); Post post = new Post(userJpaRepo.findByUid(uid).orElseThrow(CUserNotFoundException::new), board, paramsPost.getAuthor(), paramsPost.getTitle(), paramsPost.getContent()); return postJpaRepo.save(post); } @@ -72,6 +76,9 @@ public class BoardService { if (!uid.equals(user.getUid())) throw new CNotOwnerException(); + // 금칙어 체크 + checkForbiddenWord(paramsPost.getContent()); + // 영속성 컨텍스트의 변경감지(dirty checking) 기능에 의해 조회한 Post내용을 변경만 해도 Update쿼리가 실행됩니다. post.setUpdate(paramsPost.getAuthor(), paramsPost.getTitle(), paramsPost.getContent()); cacheSevice.deleteBoardCache(post.getPostId(), post.getBoard().getName()); @@ -88,4 +95,11 @@ public class BoardService { cacheSevice.deleteBoardCache(post.getPostId(), post.getBoard().getName()); return true; } + + public void checkForbiddenWord(String word) { + List forbiddenWords = Arrays.asList("개새끼", "쌍년", "씨발"); + Optional forbiddenWord = forbiddenWords.stream().filter(word::contains).findFirst(); + if(forbiddenWord.isPresent()) + throw new CForbiddenWordException(forbiddenWord.get()); + } } diff --git a/src/main/resources/i18n/exception_en.yml b/src/main/resources/i18n/exception_en.yml index 11fc40f..e60e0cc 100644 --- a/src/main/resources/i18n/exception_en.yml +++ b/src/main/resources/i18n/exception_en.yml @@ -24,4 +24,7 @@ notOwner: msg: "You are not the owner of this resource." resourceNotExist: code: "-1007" - msg: "This resource does not exist." \ No newline at end of file + msg: "This resource does not exist." +forbiddenWord: + code: "-1008" + msg: "forbidden words ({0}) are included in the input." \ No newline at end of file diff --git a/src/main/resources/i18n/exception_ko.yml b/src/main/resources/i18n/exception_ko.yml index a1537c8..8d51c03 100644 --- a/src/main/resources/i18n/exception_ko.yml +++ b/src/main/resources/i18n/exception_ko.yml @@ -24,4 +24,7 @@ notOwner: msg: "해당 자원의 소유자가 아닙니다." resourceNotExist: code: "-1007" - msg: "요청한 자원이 존재 하지 않습니다." \ No newline at end of file + msg: "요청한 자원이 존재 하지 않습니다." +forbiddenWord: + code: "-1008" + msg: "입력한 내용에 금칙어({0})가 포함되어 있습니다." \ No newline at end of file