# ์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ(Interceptor)
Study Repository

์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ(Interceptor)

by rlaehddnd0422

์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ

์ž๋ฐ” ํ‘œ์ค€์ŠคํŽ™์ด ์ œ๊ณตํ•˜๋Š” ์„œ๋ธ”๋ฆฟ ํ•„ํ„ฐ์ฒ˜๋Ÿผ ์›น๊ณผ ๊ด€๋ จ๋œ ๊ณตํ†ต ๊ด€์‹ฌ์‚ฌํ•ญ์„ ํšจ๊ณผ์ ์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค.

 

์ธํ„ฐ์…‰ํ„ฐ ํŠน์ง•

  • ์ธํ„ฐ์…‰ํ„ฐ๋Š” ์„œ๋ธ”๋ฆฟ ํ•„ํ„ฐ์˜ ํ๋ฆ„๊ณผ ๋น„์Šทํ•œ ํ๋ฆ„์„ ๊ฐ€์ ธ๊ฐ‘๋‹ˆ๋‹ค.
ํ•„ํ„ฐ : HTTP Request โ–ถ๏ธŽ WAS โ–ถ๏ธŽ ํ•„ํ„ฐ โ–ถ๏ธŽ ์„œ๋ธ”๋ฆฟ( ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ, ๋””์ŠคํŒจ์ณ ์„œ๋ธ”๋ฆฟ ) โ–ถ๏ธŽ ์ปจํŠธ๋กค๋Ÿฌ  
์ธํ„ฐ์…‰ํ„ฐ : HTTP Request โ–ถ๏ธŽ WAS โ–ถ๏ธŽ ์„œ๋ธ”๋ฆฟ โ–ถ๏ธŽ ์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ โ–ถ๏ธŽ ์ปจํŠธ๋กค๋Ÿฌ
  • ์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ์—๋„ URL ํŒจํ„ด์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์„œ๋ธ”๋ฆฟ URL ํŒจํ„ด๊ณผ ๋‹ค๋ฅด๊ณ , ๋งค์šฐ ์ •๋ฐ€ํ•˜๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ธํ„ฐ์…‰ํ„ฐ ๋˜ํ•œ ์ฒด์ธ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์ค‘๊ฐ„์— ์ธํ„ฐ์…‰ํ„ฐ๋ฅผ ์ž์œ ๋กญ๊ฒŒ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ (ex)๋กœ๊ทธ ์š”์ฒญ ์ธํ„ฐ์…‰ํ„ฐ -> ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ ์ธํ„ฐ์…‰ํ„ฐ)

์„œ๋ธ”๋ฆฟ ํ•„ํ„ฐ vs ์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ  

  • ํ•„ํ„ฐ๋Š” ์„œ๋ธ”๋ฆฟ์— ์š”์ฒญ์ด ์ „๋‹ฌ๋˜๊ธฐ ์ „๊ณผ ํ›„์— ๋ถ€๊ฐ€์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • ์ธํ„ฐ์…‰ํ„ฐ๋Š” ์„œ๋ธ”๋ฆฟ์— ์š”์ฒญ์ด ์ „๋‹ฌ๋˜๊ณ  ๋‚˜์„œ ์„œ๋ธ”๋ฆฟ์ด ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์ „๊ณผ ํ˜ธ์ถœํ•˜๊ณ  ๋‚œ ํ›„์— ์š”์ฒญ์— ๋Œ€ํ•œ ๋ถ€๊ฐ€์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ ์ธํ„ฐํŽ˜์ด์Šค

์Šคํ”„๋ง์˜ ์ธํ„ฐ์…‰ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด HandlerInterceptor ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

public interface HandlerInterceptor {

   default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
         throws Exception {

      return true;
   }

   default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
         @Nullable ModelAndView modelAndView) throws Exception {
   }

 
   default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
         @Nullable Exception ex) throws Exception {
   }

}

์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ ์š”์ฒญ ํ๋ฆ„

preHandle()

  • ์ปจํŠธ๋กค๋Ÿฌ(ํ•ธ๋“ค๋Ÿฌ) ํ˜ธ์ถœ ์ „์— ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. (์—„๋ฐ€ํžˆ ๋งํ•˜๋ฉด ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ ํ˜ธ์ถœ ์ „)
  • preHandle()์˜ ๋ฆฌํ„ด๊ฐ’์ด true์ด๋ฉด ๋‹ค์Œ์œผ๋กœ ์ง„ํ–‰, false๋ฉด ๋” ์ด์ƒ ์ง„ํ–‰ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

postHandle()

  • ์ปจํŠธ๋กค๋Ÿฌ(ํ•ธ๋“ค๋Ÿฌ) ํ˜ธ์ถœ ํ›„์— ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ( ์—„๋ฐ€ํžˆ ๋งํ•˜๋ฉด ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ ํ˜ธ์ถœ ํ›„)
  • ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ์—๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๊ณ  afterCompletion์œผ๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.
  • ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ModelAndView๊ฐ€ ๋„˜์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋ธ์ด๋‚˜ ๋ทฐ์— ์ถ”๊ฐ€์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

afterCompletion()

  • ๋ทฐ๊ฐ€ ๋ Œ๋”๋ง ๋œ ์ดํ›„ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
  • ํ•ญ์ƒ ํ˜ธ์ถœ๋˜๊ธฐ ๋•Œ๋ฌธ์— Exception์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์–ด๋–ค Exception์ด ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ๋กœ๊ทธ๋กœ ์ถœ๋ ฅํ•  ์ˆ˜๋„ ์žˆ๊ณ , Exception์„ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

  •  WebMvcConfigurer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ addInterceptors(InterceptorRegistry registry)๋ฅผ Overrideํ•ฉ๋‹ˆ๋‹ค.
  • ์ด ๋•Œ registry์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์ธํ„ฐ์…‰ํŠธ๋ฅผ ์ถ”๊ฐ€, ์ ์šฉ์ˆœ์„œ, ์ ์šฉ URL ํŒจํ„ด, ์˜ˆ์™ธ URL ํŒจํ„ด์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
registry.addInteceptor(์ถ”๊ฐ€ํ•  ์ธํ„ฐ์…‰ํ„ฐ)
        .order(์ ์šฉ ์ˆœ์„œ)
        .addPatterns("์ ์šฉํ•  URL ํŒจํ„ด")
        .excludePathPatterns("์ ์šฉํ•˜์ง€ ์•Š์„ ์˜ˆ์™ธ URL ํŒจํ„ด")

 

๋กœ๊ทธ์š”์ฒญ์— ๋Œ€ํ•œ ์ธํ„ฐ์…‰ํ„ฐ

@Slf4j
public class LogInterceptor implements HandlerInterceptor {
  • HandlerInterceptor ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„
public static final String LOG_ID = "logId";

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception
{
    log.info("------------------------------");
    String requestURI = request.getRequestURI();
    String uuid = UUID.randomUUID().toString();
    request.setAttribute(LOG_ID,uuid);

    if (handler instanceof HandlerMethod)
    {
        HandlerMethod hm = (HandlerMethod) handler;
        Method method = hm.getMethod();
        log.info("Method {}",method);
    }

    log.info("REQUEST [{}][{}][{}]", uuid, requestURI, handler);
    return true;
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
        throws Exception
{
    log.info("postHandle [{}]", modelAndView);
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
        throws Exception
{
    String requestURI = request.getRequestURI();
    String logId = (String)request.getAttribute(LOG_ID);
    log.info("RESPONSE [{}][{}]", logId, requestURI);

    // ์ปจํŠธ๋กค๋Ÿฌ๋กœ๋ถ€ํ„ฐ ์˜ˆ์™ธ ๋„˜๊ฒจ ๋ฐ›์•˜์„ ๊ฒฝ์šฐ
    if (ex != null) {
        log.error("afterCompletion error!!", ex);
    }

    log.info("------------------------------");
}

 

preHandle()

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception
{
    log.info("------------------------------");
    String requestURI = request.getRequestURI();
    String uuid = UUID.randomUUID().toString();
    request.setAttribute(LOG_ID,uuid);

    if (handler instanceof HandlerMethod)
    {
        HandlerMethod hm = (HandlerMethod) handler;
        Method method = hm.getMethod();
        log.info("handler {}, Method {}",handler,method);
    }

    log.info("REQUEST [{}][{}][{}]", uuid, requestURI, handler);
    return true;
}
  • ํ•„ํ„ฐ์—์„œ๋Š” ์ง€์—ญ๋ณ€์ˆ˜๋กœ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ์ธํ„ฐ์…‰ํ„ฐ๋Š” ํ˜ธ์ถœ ์‹œ์ ์ด ์™„์ „ํžˆ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, preHandle์—์„œ ์ง€์ •ํ•œ ๊ฐ’์„ postHandle, afterCompletion์—์„œ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์–ด๋”˜๊ฐ€์— ๋‹ด์•„๋‘์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. LogInterceptror๋„ ์‹ฑ๊ธ€ํ†ค์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์œ„ํ—˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— request์— ๋‹ด์•„๋‘์—ˆ์Šต๋‹ˆ๋‹ค.
  • HandlerMethod : ์Šคํ”„๋ง์—์„œ @Controller๋‚˜ @RequestMapping์„ ํ™œ์šฉํ•œ ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์ด ๊ฒฝ์šฐ ํ•ธ๋“ค๋Ÿฌ ์ •๋ณด๋กœ HandlerMethod๊ฐ€ ๋„˜์–ด์˜ต๋‹ˆ๋‹ค.
  • ResourceHttpRequestHandler :  @Controller๊ฐ€ ์•„๋‹Œ ์ •์  ๋ฆฌ์†Œ์Šค๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ๊ฒฝ์šฐ ResourceHttpRequestHandler๊ฐ€ ํ•ธ๋“ค๋Ÿฌ ์ •๋ณด๋กœ ๋„˜์–ด์˜ต๋‹ˆ๋‹ค.

์ธํ„ฐ์…‰ํ„ฐ ์ ์šฉ

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new LogInterceptor())
            .order(1)
            .addPathPatterns("/**")
            .excludePathPatterns("/css/**","/*.ico","/error");

 

๋กœ๊ทธ์ธ ์ธ์ฆ ์ฒดํฌ ์ธํ„ฐ์…‰ํ„ฐ

public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession(false);
        if(session==null || session.getAttribute(SessionConst.LOGIN_MEMBER) == null)
        {
            response.sendRedirect("/login?redirectURL" + request.getRequestURI());
            return false;
        }
        return true;
    }
}

preHandle์—์„œ ์„ธ์…˜์„ ํ†ตํ•ด ์„ธ์…˜์„ ์–ป์–ด ์„ธ์…˜์ด ์•„์˜ˆ ์กด์žฌํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ๋กœ๊ทธ์ธ ๋ฉค๋ฒ„์— ๋Œ€ํ•œ ์„ธ์…˜์ด ์—†์œผ๋ฉด ๋ฆฌ๋‹ค์ด๋ ‰์…˜

+ redirectURL์„ ์ฟผ๋ฆฌํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ฒจ ๋กœ๊ทธ์ธ ์ดํ›„ ์ฒ˜์Œ์— ์š”์ฒญํ•œ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋„๋ก ์„ค์ •

  • true : ์ปจํŠธ๋กค๋Ÿฌ ํ˜ธ์ถœ
  • false:  ์ปจํŠธ๋กค๋Ÿฌ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  ์ข…๋ฃŒ

์ธํ„ฐ์…‰ํ„ฐ ์ ์šฉ

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new LogInterceptor())
            .order(1)
            .addPathPatterns("/**")
            .excludePathPatterns("/css/**","/*.ico","/error");

    registry.addInterceptor(new LoginCheckInterceptor())
            .order(2)
            .addPathPatterns("/**")
            .excludePathPatterns("/","/login","/logout","/members/add","/css/**","/*.ico","/error");
}

 

<์ •๋ฆฌ>

  1. ์„œ๋ธ”๋ฆฟ ํ•„ํ„ฐ์™€ ์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ๋Š” ์›น๊ณผ ๊ด€๋ จ๋œ ๊ณตํ†ต ๊ด€์‹ฌ์‚ฌ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค.
  2. ๊ฐœ๋ฐœ์ž ์ž…์žฅ์—์„œ๋Š” ์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ๊ฐ€ ํ•„ํ„ฐ๋ณด๋‹ค ํ›จ์”ฌ ํŽธ๋ฆฌํ•˜๋ฏ€๋กœ ์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ๋ฅผ ์“ฐ๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•ฉ์‹œ๋‹ค.

<์ฐธ๊ณ ์ž๋ฃŒ>

https://blog.naver.com/PostView.naver?blogId=sosow0212&logNo=222869514885&navType=by 

 

[Spring] ์„œ๋ธ”๋ฆฟ ํ•„ํ„ฐ & ์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ

ํ˜„์žฌ ์ง„ํ–‰ ์ค‘์ธ ๋ชจ๋“  ํ”„๋กœ์ ํŠธ์—์„œ ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ๋Š” ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ ์ œ๊ฐ€ ๋งŒ๋“  ์Šคํ”„๋ง ํ”„๋กœ์ ํŠธ์—์„œ ...

blog.naver.com

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-2/dashboard

 

์Šคํ”„๋ง MVC 2ํŽธ - ๋ฐฑ์—”๋“œ ์›น ๊ฐœ๋ฐœ ํ™œ์šฉ ๊ธฐ์ˆ  - ์ธํ”„๋Ÿฐ | ๊ฐ•์˜

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ์— ํ•„์š”ํ•œ ๋ชจ๋“  ์›น ๊ธฐ์ˆ ์„ ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ดํ•ดํ•˜๊ณ , ์™„์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. MVC 2ํŽธ์—์„œ๋Š” MVC 1ํŽธ์˜ ํ•ต์‹ฌ ์›๋ฆฌ์™€ ๊ตฌ์กฐ ์œ„์— ์‹ค๋ฌด ์›น ๊ฐœ๋ฐœ์— ํ•„์š”ํ•œ ๋ชจ๋“  ํ™œ์šฉ ๊ธฐ์ˆ ๋“ค์„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ

www.inflearn.com

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ