/** 
 * BoardPreventSqlInjectionAop.java
 * @author 임새미 
 * @since 2016. 10. 19.
 *
 *  	 수정일                     수정자                                       수정내용
 *  -------------	--------	---------------------------
 *  2016. 10. 19. 	    임새미				       최초생성
 * 
 */
package kcg.faics.board.aop;

import java.lang.reflect.Field;

import kcg.faics.cmmn.CommonsUtils;

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

/** 
 * BoardPreventSqlInjectionAop.java
 * @author 임새미 
 * @since 2016. 10. 19.
 *
 *  	 수정일                     수정자                                       수정내용
 *  -------------	--------	---------------------------
 *  2016. 10. 19. 	    임새미				       최초생성
 * 
 */
@Aspect
@Component
public class BoardPreventSqlInjectionAop {

	// id를 필드로 가지고 있는 객체를 파라미터로 받는 메소드만 걸림
	@Before("execution(public * kcg.faics.board.service.impl.*Mapper.*(kcg.faics.board.vo.BoardSearchVO))"
			+ "|| execution(public * kcg.faics.board.service.impl.*Mapper.*(kcg.faics.board.vo.BoardVO))"
			+ "|| execution(public * kcg.faics.board.service.impl.*Mapper.*(kcg.faics.board.vo.BoardFileVO))"
			+ "|| execution(public * kcg.faics.board.service.impl.*Mapper.*(kcg.faics.board.vo.BoardCommentVO))")
	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();
			}
		}
    }
}
