์ƒˆ์†Œ์‹

Back-End/Spring

[Spring] Filter, Interceptor, AOP ์ •๋ฆฌ

  • -

๊ฐœ์š”

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•˜๋ฉด ํŠน์ • ๋‹จ๊ณ„์—์„œ ์ž‘๋™ํ•˜๊ฑฐ๋‚˜, ์—ฌ๋Ÿฌ ๊ณณ์—์„œ ๋ฐ˜๋ณต๋˜๋Š” ๋กœ์ง์„ ๊ณตํ†ต์œผ๋กœ ๊ด€๋ฆฌํ•ด์•ผ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด Filter , Interceptor, AOP ์ž…๋‹ˆ๋‹ค.

๋ชจ๋“  ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์— ๊ณตํ†ต์œผ๋กœ ์ ์šฉ๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ค‘๋ณต์ด ๋Š˜์–ด๋‚˜๊ณ , ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์–ด๋ ค์›Œ์ง‘๋‹ˆ๋‹ค. ์ด ์„ธ ๊ธฐ์ˆ ์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ณ„์ธต์—์„œ ์ค‘๋ณต๋˜๋Š” ๋กœ์ง์„ ์ œ๊ฑฐํ•˜๊ณ , ํŠน์ • ๋‹จ๊ณ„์—์„œ์˜ ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ€๋กœ์ฑ„๊ฑฐ๋‚˜ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์ด ๊ธ€์—์„œ๋Š” ์ด ์„ธ ๊ฐ€์ง€ ๊ธฐ์ˆ ์˜ ์ •์˜์™€ ํŠน์ง•, ๊ฐ ์‚ฌ์šฉ ์ผ€์ด์Šค๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์‹คํ–‰ ์‹œ์ 

์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด Request → Filter → Servlet → Interceptor → AOP → Controller ์ˆœ์œผ๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ Filter๋Š” Spring๊ณผ ๋ฌด๊ด€ํ•œ Web Context์— ์†ํ•˜๋ฉฐ Interceptor์™€ AOP๋Š” Spring Context์— ์†ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์‘๋‹ต์€ ์—ญ์ˆœ์ธ Controller → AOP → Interceptor → Filter → Response ์ˆœ์œผ๋กœ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

Filter

๊ฐœ๋…

Filter๋Š” Spring ์ปจํ…Œ์ด๋„ˆ์™€ ๋ฌด๊ด€ํ•œ ์›น ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๋ฉฐ, DispatcherServlet ์ „ํ›„๋กœ ์ฒ˜๋ฆฌ ์ž‘์—…์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

๊ตฌํ˜„

public interface Filter {
public default void init(FilterConfig filterConfig) throws ServletException
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
public default void destroy() {}
}

init

: ํ•„ํ„ฐ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์„œ๋น„์Šค์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

doFilter

: DispatcherServlet์— ์ „๋‹ฌ๋˜๊ธฐ ์ „ ์›น ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ์‹คํ–‰, ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ๋œ FilterChain์€ doFilter๋ฅผ ํ†ตํ•ด ๋‹ค์Œ ๋Œ€์ƒ์œผ๋กœ ์š”์ฒญ์„ ์ „๋‹ฌ

destory

: ํ•„ํ„ฐ ๊ฐ์ฒด ์‚ญ์ œ, ์ž์› ๋ฐ˜ํ™˜

์‚ฌ์šฉ

Spring๊ณผ ๋ฌด๊ด€ํ•˜๊ฒŒ ์ „์—ญ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ์ž‘์—… ์˜ˆ์ปจ๋Œ€, ์ธ์ฆ ๋ฐ ์ธ๊ฐ€ ๋“ฑ์˜ ์ž‘์—…์—์„œ ์‚ฌ์šฉ๋œ๋‹ค.

Interceptor

๊ฐœ๋…

DistpatcherServlet์ด ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์ „/ํ›„๋กœ ์š”์ฒญ๊ณผ ์‘๋‹ต์„ ์ฐธ์กฐํ•˜๊ฑฐ๋‚˜ ๊ฐ€๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

DistpatcherServlet์—๋Š” N๊ฐœ์˜ Interceptor๋ฅผ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ ๊ฐœ ์ด์ƒ์˜ Interceptor๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ ์š”์ฒญ์€ Interceptor๋ฅผ ๊ฑฐ์ณ ์ปจํŠธ๋กค๋Ÿฌ์— ์ „๋‹ฌ๋˜๋ฉฐ, Interceptor๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” ์ฆ‰์‹œ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์š”์ฒญ์ด ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.

๊ตฌํ˜„

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

: ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ํ˜ธ์ถœ๋˜๊ธฐ ์ „์— ์‹คํ–‰๋˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ์™€ ๊ด€๋ จ๋˜์–ด ์ „์—ญ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ์ž‘์—… ์˜ˆ์ปจ๋Œ€, ์ธ์ฆ ๋ฐ ์ธ๊ฐ€, ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋„˜๊ฒจ์ง€๋Š” ์ •๋ณด ๊ฐ€๊ณต, ๋กœ๊ทธ ํ™•์ธ ๋“ฑ์˜ ์ž‘์—…์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

AOP(Aspect-Oriented Programming)

๊ฐœ๋…

AOP๋Š” ๊ด€์‹ฌ์‚ฌ์˜ ๋ถ„๋ฆฌ๋ฅผ ํ†ตํ•ด ์ฝ”๋“œ์˜ ์ค‘๋ณต์„ ์ค„์ด๊ณ , ๋ชจ๋“ˆํ™”๋ฅผ ํ–ฅ์ƒ์‹œํ‚ค๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํŒจ๋Ÿฌ๋‹ค์ž„์ž…๋‹ˆ๋‹ค.

์šฉ๋„

๋ฉ”์„œ๋“œ๋‚˜ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์ „์ฒด ๊ณตํ†ต ์š”์†Œ๋Š” ์•„๋‹ˆ์ง€๋งŒ, ๋ถ€๋ถ„์  ๊ณตํ†ต์š”์†Œ์ผ ๊ฒฝ์šฐ ๋˜๋Š”, ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ƒ์—์„œ ์ข€ ๋” ์„ธ๋ฐ€ํ•˜๊ฒŒ ๊ณตํ†ต ์š”์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š” ์˜ˆ์ปจ๋Œ€, ๋กœ๊น…, ํŠธ๋žœ์žญ์…˜, ์—๋Ÿฌ ์ฒ˜๋ฆฌ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

Interceptor/Filter๋Š” ์ฃผ์†Œ๋กœ ๋Œ€์ƒ์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐ˜๋ฉด, AOP๋Š” PointCut์ด ์ง€์›ํ•˜๋Š” ๋ฐฉ์‹(ex. ์ฃผ์†Œ, ํŒŒ๋ผ๋ฏธํ„ฐ, ์• ๋…ธํ…Œ์ด์…˜)์œผ๋กœ ๋Œ€์ƒ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฃผ์š” ๊ฐœ๋…

  1. PointCut : Aspect๋ฅผ ์ ์šฉํ•  ํƒ€๊นƒ ๋ฉ”์„œ๋“œ ์ง€์ •์ž
  2. JoinPoint : Aspect๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์œ„์น˜
  3. Advice : PointCut์— ์ ์šฉํ•  ๋กœ์ง
  4. Aspect : Advice ๋ชจ์Œ + PointCut ๋ชจ์Œ

๋งˆ๋ฌด๋ฆฌ

Filter๋Š” ์›น ์ปจํ…Œ์ด๋„ˆ ์˜์—ญ์—์„œ ๋™์ž‘ํ•˜๋ฉฐ ์š”์ฒญ์ด DispatcherServlet์— ๋„๋‹ฌํ•˜๊ธฐ ์ „์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์‘๋‹ต ์ „/ํ›„ ์ฒ˜๋ฆฌ์— ์‚ฌ์šฉ๋˜๋ฉฐ ๋Œ€ํ‘œ์ ์œผ๋กœ ๋กœ๊น…, ์ธ์ฝ”๋”ฉ, ๋ณด์•ˆ(CORS ์„ค์ •, ์ธ์ฆ ํ™•์ธ) ๋“ฑ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

Interceptor๋Š” ํ”„๋ ˆ์ž„์›Œํฌ ๋ ˆ๋ฒจ์—์„œ ์š”์ฒญ์ด ์ปจํŠธ๋กค๋Ÿฌ์— ๋„์ฐฉํ•˜๊ธฐ ์ „์— ํŠน์ • ๋กœ์ง, ์˜ˆ์ปจ๋Œ€ ์ธ๊ฐ€, ๋กœ๊น… ๋“ฑ์— ์‚ฌ์šฉ๋œ๋‹ค. URL ํŒจํ„ด ๊ธฐ๋ฐ˜์œผ๋กœ ํŠน์ • ์š”์ฒญ์— ๋Œ€ํ•ด์„œ๋งŒ ๋™์ž‘์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

AOP๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „๋ฐ˜์— ๊ฑธ์นœ ๊ณตํ†ต์  ๊ด€์‹ฌ์‚ฌ๋ฅผ ๋ถ„๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ค. Interceptor์™€ Filter๋Š” URL๋กœ ๋Œ€์ƒ์„ ๊ตฌ๋ถ„ํ•˜์ง€๋งŒ, AOP๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ, ์• ๋…ธํ…Œ์ด์…˜ ๋“ฑ์œผ๋กœ ๋Œ€์ƒ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜Š

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๐Ÿ‘