๐ŸŒฑ Spring MVC (11) Spring Interceptor๋ฅผ ์ด์šฉํ•œ ์‚ฌ์šฉ์ž ๊ถŒํ•œ ๊ฒ€์ฆ ๋ฐ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ

Kim Dae Hyunยท2021๋…„ 7์›” 16์ผ
1
post-thumbnail

Github ์†Œ์Šค์ฝ”๋“œ

๐Ÿ”Ž Spring Interceptor์˜ ๋™์ž‘ ์‹œ์ 

๐Ÿ”Ž Interceptor ๊ตฌํ˜„

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

HandlerInterceptor์—๋Š” 3๊ฐ€์ง€ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • preHandle ๋ฉ”์„œ๋“œ๋Š” ์ปจํŠธ๋กค๋Ÿฌ ํ˜ธ์ถœ ์ „์— ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.
    ์šฐ๋ฆฌ๊ฐ€ ํ•˜๊ณ ์ž ํ•˜๋Š” ๋กœ๊ทธ์ธ ๊ฒ€์ฆ ๋ฐ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ๋Š” ์ด ๋ฉ”์„œ๋“œ์— ๊ตฌํ˜„ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ธฐ๋ณธ์ ์œผ๋กœ request, response ๊ฐ์ฒด๋ฅผ ๊ฐ–๊ณ  ์žˆ๊ณ  handler ์ฆ‰ ์ปจํŠธ๋กค๋Ÿฌ์— ๋Œ€ํ•œ ๊ฐ์ฒด๋„ ๊ฐ–๊ณ ์žˆ์Šต๋‹ˆ๋‹ค. Dispatcher servlet ์ดํ›„์— ํ˜ธ์ถœ๋˜๊ธฐ์— ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ด์ง€์š”.
  • postHandle ๋ฉ”์„œ๋“œ๋Š” ์ปจํŠธ๋กค๋Ÿฌ ํ˜ธ์ถœ ํ›„์— ์˜ˆ์™ธ๊ฐ€ ์—†์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ์ปจํŠธ๋กค๋Ÿฌ ํ˜ธ์ถœ ์งํ›„์— ํ˜ธ์ถœ๋˜์ง€๋งŒ ๋งŒ์•ฝ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค๋ฉด postHandle ๋ฉ”์„œ๋“œ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ModelAndView ๊ฐ์ฒด๋„ ๊ฐ–๊ณ ์žˆ๋Š” ๊ฒƒ์ด ํŠน์ด์  ์ž…๋‹ˆ๋‹ค.
  • afterComplete ๋ฉ”์„œ๋“œ๋Š” ์ปจํŠธ๋กค๋Ÿฌ์˜ Http ์š”์ฒญ์ด ๋ชจ๋‘ ์™„๋ฃŒ๋œ ํ›„์— ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ๋„ ๋ฐ˜๋“œ์‹œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
  • ๋•Œ๋ฌธ์— Exception ๊ฐ์ฒด๋„ ๊ฐ–๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๋ฉ”์„œ๋“œ๋Š” preHandle ๋ฉ”์„œ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์— preHandle ๋ฉ”์„œ๋“œ๋งŒ ๊ตฌํ˜„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. HandlerInterceptor์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋Š” default ํ‚ค์›Œ๋“œ๊ฐ€ ๋ถ™์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•„์ˆ˜ ๊ตฌํ˜„ ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.

preHandle์˜ ๊ตฌํ˜„์€ ์ด์ „ Filter์˜ doFilter๋ฉ”์„œ๋“œ์™€ ๊ฑฐ์˜ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค.
๋‹ค๋ฅธ ์ ์€ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ URI์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ์ธํ„ฐ์…‰ํ„ฐ ๊ตฌํ˜„๋ถ€์—์„œ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์  ์ž…๋‹ˆ๋‹ค.

@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {

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

        String requestURI = request.getRequestURI();
        log.info("์ธ์ฆ ์š”์ฒญ {}", requestURI);

        HttpSession session = request.getSession();
        if (session == null || session.getAttribute(SessionConst.LOGIN_MEMBER) == null) {
            log.info("์ธ์ฆ ์‹คํŒจ");
            response.sendRedirect("/login?redirectURL=" + requestURI);
            return false; // ๋” ์ด์ƒ ์ง„ํ–‰ โŒ
        }
        return true;
    }
}

HttpServletRequest ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ session ์ •๋ณด๋ฅผ ๋ฐ›์•„์™€ ๊ถŒํ•œ์„ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค.

  • ๊ฒ€์ฆ์— ์‹คํŒจ์‹œ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ์ด๋™์ „ ์ง์ „ URI์ •๋ณด์™€ ํ•จ๊ป˜ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ redirect ํ›„ false๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฒ€์ฆ์— ์„ฑ๊ณต์‹œ true๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

true ๋ฆฌํ„ด์‹œ ๋‹ค์Œ ์ธํ„ฐ์…‰ํ„ฐ ํ˜น์€ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
false ๋ฆฌํ„ด์‹œ ํ˜„์žฌ ์ธํ„ฐ์…‰ํ„ฐ์—์„œ ์ง„ํ–‰์„ ๋ฉˆ์ถฅ๋‹ˆ๋‹ค. (์šฐ๋ฆฌ๋Š” false ๋ฆฌํ„ด ์ „ redirectํ•ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฉˆ์ถ”์ง€ ์•Š๊ณ  redirect ์ดํ›„ ํ๋ฆ„์„ ๋”ฐ๋ผ๊ฐ‘๋‹ˆ๋‹ค.)


๐Ÿ”Ž Interceptor ๋“ฑ๋ก

WebMvcConfigurer๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์„ค์ • ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
(์ปดํฌ๋„ŒํŠธ ์Šค์บ”์˜ ๋Œ€์ƒ์ด ๋˜๊ธฐ ์œ„ํ•ด @Configuration์„ ๋ถ™์—ฌ์ค๋‹ˆ๋‹ค.)

addInterceptors(InterceptorRegistry) ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•ฉ๋‹ˆ๋‹ค.

@Override
public void addInterceptors(InterceptorRegistry registry) {

    registry.addInterceptor(new LoginCheckInterceptor())
            .order(1)
            .addPathPatterns("/**")
            .excludePathPatterns("/", "/members/add", "/login", "/logout",  "/css/**");
}
  • order๋Š” ์ธํ„ฐ์…‰ํ„ฐ์˜ ์ˆœ์„œ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ถŒํ•œ ๊ฒ€์ฆ์„ ์œ„ํ•œ ์ธํ„ฐ์…‰ํ„ฐ๋ณด๋‹ค ์•ž์— ๋™์ž‘์‹œํ‚ค๊ณ  ์‹ถ์€ ์ธํ„ฐ์…‰ํ„ฐ๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ทธ ์ธํ„ฐ์…‰ํ„ฐ์˜ order๋ฅผ 1๋กœํ•˜๊ณ  ๋กœ๊ทธ์ธ ๊ฒ€์ฆ ์ธํ„ฐ์…‰ํ„ฐ์˜ order๋ฅผ 2๋กœ ํ•ด์ค๋‹ˆ๋‹ค.
  • addPathPatterns ๋“ฑ๋กํ•˜๊ณ ์ž ํ•˜๋Š” ์ธํ„ฐ์…‰ํ„ฐ๋ฅผ ์ ์šฉ์‹œํ‚ฌ ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
    ๋ณดํ†ต ์Šคํ”„๋ง ๊ธฐ์ˆ ์—์„œ ์ „์ฒด ๊ฒฝ๋กœ๋Š” /** ์ž…๋‹ˆ๋‹ค. /*๊ฐ€ ์•„๋‹Œ ๊ฒƒ์— ์œ ์˜ํ•ฉ๋‹ˆ๋‹ค.
  • excludePathPatterns์—๋Š” ์ธํ„ฐ์…‰ํ„ฐ๋ฅผ ์ ์šฉ์‹œํ‚ค์ง€ ์•Š๊ณ ์ž ํ•˜๋Š” ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
    • ์ด๋Ÿฐ ๋ฉ”์„œ๋“œ๊ฐ€ ์ง€์›๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ธํ„ฐ์…‰ํ„ฐ ๊ตฌํ˜„๋ถ€์—์„œ ๊ฒฝ๋กœ์— ๋Œ€ํ•œ ๊ฒ€์ฆ์ด ๋ถˆํ•„์š”ํ•œ ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

์œ„์™€ ๊ฐ™์ด ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค๋ฉด ๋ชจ๋“  ๊ฒฝ๋กœ์— ๋Œ€ํ•ด ๋“ฑ๋กํ•œ ์ธํ„ฐ์…‰ํ„ฐ๋ฅผ ์ ์šฉ์‹œํ‚ค๊ณ  / , /members/add, /login, /logout, /css/** ์— ๋Œ€ํ•ด ์ธํ„ฐ์…‰ํ„ฐ๋ฅผ ์ ์šฉ์‹œํ‚ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ธํ”„๋Ÿฐ ๊น€์˜ํ•œ๋‹˜์˜ ์Šคํ”„๋ง MVC 2ํŽธ ์„ ์ˆ˜๊ฐ•ํ•˜๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€