It's Repository

blog.selpa.co.kr

포토로그



java interceptor 예제 JAVA

<bean id="commonInterceptor" class="com.acc.interceptor.CommonInterceptor" />

<bean id="commonAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
<property name="advice" ref="commonInterceptor" />  
<property name="pattern" value=".*Controller.*" />
</bean>    







-------------------------------
package goo.interceptor;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import goo.service.GooService;
import goo.service.GooVO;

public class CommonInterceptor implements MethodInterceptor {
@Autowired
private GooService gooService;
@Override
public Object invoke(MethodInvocation invocation) throws Throwable
{
    Class[] params = invocation.getMethod().getParameterTypes();
   
    HttpServletRequest request = null;
    HttpServletResponse response = null;
    DefaultMultipartHttpServletRequest multipartRequest = null;
   
    if(params != null)
    {
    /* request, response 찾기 */
    for(int i=0, cnt=params.length; i<cnt; i++)
    {
    Object param = invocation.getArguments()[i];

    if(param == null) continue;
    if("DefaultMultipartHttpServletRequest".equals(param.getClass().getSimpleName()))
    {
    multipartRequest = (DefaultMultipartHttpServletRequest)invocation.getArguments()[i];
    request = (HttpServletRequest)multipartRequest.getRequest();
    }
    if(ReflectionUtil.hasInterfaceBySimpleName(param.getClass(), "HttpServletRequest"))
    {
    request = (HttpServletRequest)invocation.getArguments()[i];
    }
   
    if(ReflectionUtil.hasInterfaceBySimpleName(param.getClass(), "HttpServletResponse"))
    {
    response = (HttpServletResponse)invocation.getArguments()[i];
    }
    }
    }
   
    /* 공통 사용 정의 */
    if(request != null)
    {
    request.setAttribute("PATH", request.getContextPath());
    }
   
    /* 권한 체크 */
    if(params != null)
    {
    for(int i=0, cnt=params.length; i<cnt; i++)
    {
    Object param = invocation.getArguments()[i];
    if(param == null) continue;
   
    if(mediapotService == null || request == null) return new ModelAndView("block");
   
    GooVO gooVO = gooService.selectMediapotAuth(Util.getIp(request));
   
    if(collectionVO == null) return new ModelAndView("block");
   
    Method setId = ReflectionUtil.getMethodByName(param.getClass(), "setId");
   
    /* 0일 경우 전체 권한 있음 */
    if(setId != null) setId.invoke(param, gooVO.getId());
    }
    }

    Object obj = null;
try
{
obj = invocation.proceed();
}
catch(Exception e)
{
throw e;
}

return obj;
}
}

덧글

댓글 입력 영역