게시글 작성/수정시 금칙어 체크 로직 추가

This commit is contained in:
abel
2020-06-23 22:40:48 +09:00
parent 9e30c2ca80
commit 8effadb882
5 changed files with 44 additions and 2 deletions

View File

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

View File

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

View File

@@ -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<String> forbiddenWords = Arrays.asList("개새끼", "쌍년", "씨발");
Optional<String> forbiddenWord = forbiddenWords.stream().filter(word::contains).findFirst();
if(forbiddenWord.isPresent())
throw new CForbiddenWordException(forbiddenWord.get());
}
}

View File

@@ -24,4 +24,7 @@ notOwner:
msg: "You are not the owner of this resource."
resourceNotExist:
code: "-1007"
msg: "This resource does not exist."
msg: "This resource does not exist."
forbiddenWord:
code: "-1008"
msg: "forbidden words ({0}) are included in the input."

View File

@@ -24,4 +24,7 @@ notOwner:
msg: "해당 자원의 소유자가 아닙니다."
resourceNotExist:
code: "-1007"
msg: "요청한 자원이 존재 하지 않습니다."
msg: "요청한 자원이 존재 하지 않습니다."
forbiddenWord:
code: "-1008"
msg: "입력한 내용에 금칙어({0})가 포함되어 있습니다."