Data를 저장 시 저장되는 모든 정보를 이력으로 남겨 놓기 위하여 작성했습니다.


말들이 하도 많아서...ㅜㅜ



import java.lang.reflect.Method;

import java.util.Enumeration;


import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;


import org.apache.commons.lang3.ArrayUtils;

import org.apache.commons.lang3.StringUtils;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.reflect.MethodSignature;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.security.core.context.SecurityContextHolder;

import org.springframework.security.core.userdetails.UserDetails;

import org.springframework.stereotype.Component;

import org.springframework.util.ObjectUtils;

import org.springframework.web.context.request.RequestContextHolder;

import org.springframework.web.context.request.ServletRequestAttributes;


import com.duegosystem.esh.common.utils.RequestUtils;

import com.duegosystem.esh.config.aspect.operate.annotation.OperateLog;

import com.duegosystem.esh.config.aspect.operate.service.UserOpertHistService;

import com.duegosystem.esh.config.aspect.operate.vo.OperateHistVo;

import com.duegosystem.esh.config.security.vo.UserVo;


/**

 * @author Duego-Choi

 *

 */

@Aspect

@Component

public class OperateAdviceLogging {


private static final Logger logger = LoggerFactory.getLogger(OperateAdviceLogging.class);


@Resource(name = "userOpertHistService")

private UserOpertHistService userOpertHistService;


/**

* 등록 수정 삭제 시에만 적용합니다.

*

* @param joinPoint

* @return

* @throws Throwable

*/

@Around("execution(* com.duegosystem..*Controller.insert*(..)) || "

+ "execution(* com.duegosystem..*Controller.update*(..)) || "

+ "execution(* com.duegosystem..*Controller.delete*(..))")

public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {


logger.debug("Operate History Logging save processing.......................................");


Object principal = (Object) SecurityContextHolder.getContext().getAuthentication().getPrincipal();


HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())

.getRequest();


String httpMethod = request.getMethod();

MethodSignature signature = (MethodSignature) joinPoint.getSignature();

Method method = signature.getMethod();


logger.debug("httpMethod : " + httpMethod);

logger.debug("Class Method : " + method);


OperateHistVo operateHistVo = null;


/*

* 저장 수정 삭제 일경우만 사용하도록 하기 위해 POST 형식일 때만 적용되어지도록 처리

*/

if ("POST".equals(httpMethod)) {


if ((principal instanceof UserDetails)) {


operateHistVo = new OperateHistVo();


// 사용자 정보

UserVo user = (UserVo) principal;

logger.debug("empno : " + user.getEmpno());

logger.debug("emplNm : " + user.getEmplNm());


operateHistVo.setOpertId(user.getEmpno());

operateHistVo.setOpertNm(user.getEmplNm());


// 작업자 아이피

String ipAddress = RequestUtils.getRemoteIP(request);

logger.debug("ipAddress : " + ipAddress);

operateHistVo.setOpertIp(ipAddress);


// 접근 경로(메뉴 패턴)

String requestUri = request.getRequestURI();

String contextPath = request.getContextPath();

String pattern = requestUri.replaceAll("(^" + contextPath + ")|((\\.[^\\.]*)$)|((/[^/]+){1}/*$)", "")

+ "/";

logger.debug("Url pattern : " + pattern);

operateHistVo.setPattern(pattern);


String operateFile = requestUri.replace(contextPath, "").replace(pattern, "");

String operateType = "insert";

if (operateFile.startsWith("update")) {

operateType = "update";

} else if (operateFile.startsWith("delete")) {

operateType = "delete";

}

logger.debug("Operate Type : " + operateType);

operateHistVo.setOpertTy(operateType);


/*

* OperateLog Annotation을 통해서 등록하고자 하는 파라메터 또는 등록지 말아야 할 파라메터를

* 지정하였을 경우 처리합니다.

*

* 우선 순위는 등록해야할 파라메터가 있는 경우가 우선입니다.

*/

Enumeration<String> params = request.getParameterNames();


if (method.isAnnotationPresent(OperateLog.class)) {

OperateLog operateLog = method.getDeclaredAnnotation(OperateLog.class);


String[] saveParams = operateLog.params();

String[] notSaveParams = operateLog.notSaveParam();


// 저장하고자 하는 파라메타명을 설정하였을 경우

if (!ObjectUtils.isEmpty(saveParams)) {

while (params.hasMoreElements()) {

String paramName = (String) params.nextElement();

if (ArrayUtils.contains(saveParams, paramName)) {

if (!StringUtils.isEmpty(request.getParameter(paramName))) {

operateHistVo.addParam(paramName, request.getParameter(paramName));

}

}

}


// 저장하지 않을 파라메타명을 성정하였을 경우

} else if (!ObjectUtils.isEmpty(notSaveParams)) {

while (params.hasMoreElements()) {

String paramName = (String) params.nextElement();

if (ArrayUtils.contains(notSaveParams, paramName)) {

continue;

} else {

if (!StringUtils.isEmpty(request.getParameter(paramName))) {

operateHistVo.addParam(paramName, request.getParameter(paramName));

}

}

}

} else {

while (params.hasMoreElements()) {

String paramName = (String) params.nextElement();

if (!StringUtils.isEmpty(request.getParameter(paramName))) {

operateHistVo.addParam(paramName, request.getParameter(paramName));

}

}

}


} else {

while (params.hasMoreElements()) {

String paramName = (String) params.nextElement();

if (!StringUtils.isEmpty(request.getParameter(paramName))) {

operateHistVo.addParam(paramName, request.getParameter(paramName));

}

}

}


userOpertHistService.insert(operateHistVo);

}

}


Object obj = joinPoint.proceed();


/*

* Method에 해당하는 처리가 모두 끝난 후 후처리로 성공 상태 값을 Y로 바꿔준다.

*/

if ("POST".equals(httpMethod)) {

if (!ObjectUtils.isEmpty(operateHistVo)) {

try {

userOpertHistService.updateSuccess(operateHistVo.getOpertHistSeq());

} catch (Exception e) {

logger.error("OperateHist Advice Processing Error......!!!", e);

}

}

}


return obj;

}

}


+ Recent posts