package kcg.imis.board.aop;

import java.lang.reflect.Field;

import kcg.imis.cmmn.CommonsUtils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
  * @FileName : BoardPreventSqlInjectionAop.java
  * @Project : 국제해양프로젝트
  * @Date : 2018. 3. 21. 
  * @작성자 : Moon
  * @변경이력 : 
  * @프로그램 설명 : 
 */
@Aspect
@Component
public class BoardPreventSqlInjectionAop {

	// id를 필드로 가지고 있는 객체를 파라미터로 받는 메소드만 걸림
	@Before("execution(public * kcg.faics.board.service.impl.*Mapper.*(kcg.imis.board.vo.BoardSearchVO))"
			+ "|| execution(public * kcg.faics.board.service.impl.*Mapper.*(kcg.imis.board.vo.BoardVO))")
	public void beforeTargetMethod(JoinPoint thisJoinPoint) {
		Object[] signatureArgs = thisJoinPoint.getArgs();
		for (Object signatureArg: signatureArgs) {
			
			Class<?> clz = signatureArg.getClass();
			try {
				Field field = clz.getDeclaredField("id");
				field.setAccessible(true);
				
				String id = (String) field.get(signatureArg);
				id = CommonsUtils.sqlInjectionFilter(id);
				
				field.set(signatureArg, id);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
    }
}
