특정 URL호출을 가로채어서 추가적인 로직을 수행후 진행시키는 개념이다.
1. dispatcher-context.xml
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**/upload/download.do"/>
<mvc:mapping path="/**/upload/downloadZip.do"/>
<bean id="attachmentLogInterceptor" class="smartsuite.log.spring.accesslog.AttachmentLogInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
2. AttachmentLogInterceptor.java
package smartsuite.log.spring.accesslog;
import java.util.Map;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.google.common.collect.Maps;
import smartsuite.app.common.shared.SharedService;
import smartsuite.upload.spring.management.Management;
public class AttachmentLogInterceptor extends HandlerInterceptorAdapter
{
@Inject
SharedService sharedService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
{
HandlerMethod handlerMethod = null;
try
{
handlerMethod = (HandlerMethod)handler;
this.doAttachmentlog(request, handlerMethod);
}
catch (Exception e)
{
super.postHandle(request, response, handler, modelAndView);
}
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) throws Exception
{
super.afterCompletion(request, response, handler, e);
}
private void doAttachmentlog(HttpServletRequest request, HandlerMethod handlerMethod) throws Exception
{
Management management = new Management();
String[] ids = request.getParameterValues("id");
for (String id : ids)
{
id = management.getFileItemEncryptor().decrypt(id);
Map<String, Object> _param = Maps.newHashMap();
_param.put("att_id" , id);
_param.put("client_ip" , sharedService.getClientIpAddress(request));
sharedService.insFileDownloadLog(_param);
}
}
}
3. SharedService.java
/**
* KPOPIF.IF_TI003T_PMANAGER 다운로드 로그 기록
*
* @author :
* @param : param
* @return : Map
* @Date :
* @Method Name : insFileDownloadLog
*/
public void insFileDownloadLog(Map<String, Object> param)
{
sqlSession.insert("shared.insFileDownloadLog", param);
}
4. shared.xml
<insert id="insFileDownloadLog">
<![CDATA[
/* shared.insFileDownloadLog */
INSERT INTO KPOPIF.IF_TI003T_PMANAGER ( SYS_ID
, USER_ID
, USE_TIME
, PROJ_CD
, DOCU_NM
, DOCU_NO
, REV_NO
, DOCU_TYPE
, READ_GB
, SECURE_LEVEL
, FILE_NAME
, FILE_SIZE
, EXCEL_ROWS
, ATT_ID
, GRP_CD
, IPADDR)
SELECT A.SYS_ID AS SYS_ID
, #{g.username} AS USER_ID
, SYSDATE AS USE_TIME
, NULL AS PROJ_CD
, A.ORGN_FILE_NM AS DOCU_NM
, NULL AS DOCU_NO
, NULL AS REV_NO
, NULL AS DOCU_TYPE
, 'FILE' AS READ_GB
, NULL AS SECURE_LEVEL
, A.ORGN_FILE_NM AS FILE_NAME
, A.ATT_FILE_SIZ AS FILE_SIZE
, NULL AS EXCEL_ROWS
, A.ATT_ID AS ATT_ID
, A.GRP_CD AS GRP_CD
, #{p.client_ip} AS IPADDR
FROM KPOP.ESAATTH A
WHERE A.SYS_ID = #{g.tenant}
AND A.ATT_ID = #{p.att_id}
]]>
</insert>