|
| 1 | +package com.rest.api.annotation.aspect; |
| 2 | + |
| 3 | +import com.rest.api.advice.exception.CForbiddenWordException; |
| 4 | +import com.rest.api.annotation.ForbiddenWordCheck; |
| 5 | +import io.micrometer.core.instrument.util.StringUtils; |
| 6 | +import org.aspectj.lang.JoinPoint; |
| 7 | +import org.aspectj.lang.annotation.Aspect; |
| 8 | +import org.aspectj.lang.annotation.Before; |
| 9 | +import org.aspectj.lang.reflect.MethodSignature; |
| 10 | +import org.springframework.stereotype.Component; |
| 11 | + |
| 12 | +import java.lang.reflect.Field; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Optional; |
| 16 | + |
| 17 | +@Aspect |
| 18 | +@Component |
| 19 | +public class ForbiddenWordCheckAspect { |
| 20 | + |
| 21 | + // 어노테이션이 설정된 메서드의 메인 프로세스가 시작되기전(Before)에 금칙어 체크 로직이 적용된다. |
| 22 | + @Before(value = "@annotation(forbiddenWordCheck)") |
| 23 | + public void forbiddenWordCheck(JoinPoint pjp, ForbiddenWordCheck forbiddenWordCheck) throws Throwable { |
| 24 | + // 금칙어를 체크할 메서드의 파라미터가 객체인지(객체.필드명) 일반 String인지에 따라 구분하여 처리한다. |
| 25 | + String[] param = forbiddenWordCheck.param().split("\\."); |
| 26 | + String paramName; |
| 27 | + String fieldName = ""; |
| 28 | + if (param.length == 2) { |
| 29 | + paramName = param[0]; |
| 30 | + fieldName = param[1]; |
| 31 | + } else { |
| 32 | + paramName = forbiddenWordCheck.param(); |
| 33 | + } |
| 34 | + // 메서드의 파라미터 이름으로 메서드의 몇번째 파라미터인지 구한다. |
| 35 | + Integer parameterIdx = getParameterIdx(pjp, paramName); |
| 36 | + if (parameterIdx == -1) |
| 37 | + throw new IllegalArgumentException(); |
| 38 | + |
| 39 | + String checkWord; |
| 40 | + // 객체내의 필드값에서 금칙어 체크 문장을 얻어내야 할 경우 |
| 41 | + if (StringUtils.isNotEmpty(fieldName)) { |
| 42 | + Class<?> clazz = forbiddenWordCheck.checkClazz(); |
| 43 | + Field field = clazz.getDeclaredField(fieldName); |
| 44 | + field.setAccessible(true); |
| 45 | + checkWord = (String) field.get(pjp.getArgs()[parameterIdx]); |
| 46 | + // 금칙어 체크 문장이 String형의 파라미터로 넘어오는 경우 |
| 47 | + } else { |
| 48 | + checkWord = (String) pjp.getArgs()[parameterIdx]; |
| 49 | + } |
| 50 | + // 체크할 문장에 금칙어가 포함되어 있는지 확인 |
| 51 | + checkForbiddenWord(checkWord); |
| 52 | + } |
| 53 | + |
| 54 | + // 메서드의 파라미터 이름으로 몇번째에 파라미터가 위치하는지 구함 |
| 55 | + private Integer getParameterIdx(JoinPoint joinPoint, String paramName) { |
| 56 | + MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); |
| 57 | + String[] parameterNames = methodSignature.getParameterNames(); |
| 58 | + for (int i = 0; i < parameterNames.length; i++) { |
| 59 | + String parameterName = parameterNames[i]; |
| 60 | + if (paramName.equals(parameterName)) { |
| 61 | + return i; |
| 62 | + } |
| 63 | + } |
| 64 | + return -1; |
| 65 | + } |
| 66 | + |
| 67 | + // 입력된 문장에 금칙어가 포함되어 있으면 Exception을 발생시킨다. |
| 68 | + private void checkForbiddenWord(String word) { |
| 69 | + List<String> forbiddenWords = Arrays.asList("개새끼", "쌍년", "씨발"); |
| 70 | + Optional<String> forbiddenWord = forbiddenWords.stream().filter(word::contains).findFirst(); |
| 71 | + if (forbiddenWord.isPresent()) |
| 72 | + throw new CForbiddenWordException(forbiddenWord.get()); |
| 73 | + } |
| 74 | +} |
0 commit comments