Filter


๐Ÿ”Ž Filter, Interceptor

๋กœ๊ทธ์ธ์„ ํ•˜์ง€ ์•Š์€ ์‚ฌ์šฉ์ž๊ฐ€ ๋ฌผ๊ฑด์„ ์ฃผ๋ฌธํ•˜๋Š” ์ผ์€ ์—†์–ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ๋กœ๊ทธ์ธ ํ•˜์ง€ ์•Š์€ ํด๋ผ์ด์–ธํŠธ๋Š” ๋‹ค์‹œ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ์ด๋™์‹œํ‚ค๋Š” ๋“ฑ

์ ์ ˆํ•œ ์กฐ์น˜๋ฅผ ์ทจํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ด๋Ÿฐ ์กฐ์น˜๋ฅผ ๋ชจ๋“  ์š”์ฒญ์— ์ฝ”๋”ฉํ•œ๋‹ค๋ฉด ๋งค์šฐ

์ƒ์‚ฐ์ ์ด์ง€ ์•Š๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„  Filter์™€ Interceptor๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด๋œ๋‹ค.

Filter๋Š” Servlet์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๊ณ ,

Interceptore๋Š” ์Šคํ”„๋ง์—์„œ ์ž์ฒด์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ์ธํ„ฐํ—ค์ด์Šค ์ด๋‹ค.


Servlet Filter

1. ํ•„ํ„ฐ์˜ ํ๋ฆ„

HTTP ์š”์ฒญ โ†’ WAS โ†’ ํ•„ํ„ฐ โ†’ (Dispatch)์„œ๋ธ”๋ฆฟ โ†’ ์ปจํŠธ๋กค๋Ÿฌ

ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ํ•„ํ„ฐ๊ฐ€ ๋จผ์ € ํ˜ธ์ถœ๋œ ํ›„, ์„œ๋ธ”๋ฆฟ์ด ํ˜ธ์ถœ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํ•„ํ„ฐ์—๋Š” ํŠน์ • URL์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


2. ํ•„ํ„ฐ์˜ ์ œํ•œ

HTTP ์š”์ฒญ โ†’ WAS โ†’ ํ•„ํ„ฐ (์ ํ•ฉํ•˜์ง€ ์•Š์€ ์š”์ฒญ์ผ ๊ฒฝ์šฐ ์—ฌ๊ธฐ์„œ ์ค‘๋‹จ)

๋งŒ์•ฝ ํ•„ํ„ฐ์—์„œ ์š”์ฒญ์„ ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ์˜ ํ๋ฆ„์€ ์œ„์™€ ๊ฐ™๋‹ค.

์ค‘๊ฐ„์— ์ข…๋ฃŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋กœ๊ทผ์ธ ํ™•์ธ ์—ฌ๋ถ€๋ฅผ ๋ณด๊ธฐ์— ์ข‹๋‹ค.


3. ํ•„ํ„ฐ ์ฒด์ธ

HTTP ์š”์ฒญ โ†’ WAS โ†’ ํ•„ํ„ฐ1 โ†’ ํ•„ํ„ฐ2 โ†’ ํ•„ํ„ฐ3 โ†’ (Dispatch)์„œ๋ธ”๋ฆฟ โ†’ ์ปจํŠธ๋กค๋Ÿฌ

ํ•„ํ„ฐ๋Š” ํ•œ ๊ฐœ ์ด์ƒ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋Œ€์‹ ์— ํ•„ํ„ฐ๋ฅผ WebConfig์— ๋“ฑ๋กํ•  ๋•Œ

๊ทธ ์ˆœ์„œ๋Š” setOrder()๋กœ ์ง€์ •ํ•ด ์ฃผ์–ด์•ผํ•œ๋‹ค.


4. Filter Interface

public interface Filter {

    public default void init(FilterConfig filterConfig) throws ServletException {}

    
    public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException;

    
	public default void destroy() {}
}

ํ•„ํ„ฐ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ๋“ฑ๋กํ•˜๋ฉด ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ•„ํ„ฐ๋ฅผ ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ•˜๊ณ , ๊ด€๋ฆฌํ•œ๋‹ค.

init()๊ณผ destory() ๋Š” default ๋ฉ”์†Œ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ override ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

doFilter()๊ฐ€ ๊ฐ€์žฅ ์ค‘์š”ํ•œ๋Œ€, ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์ด ์˜ฌ ๋•Œ๋งˆ๋‹ค ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

๋”ฐ๋ผ์„œ ์ด ๋ฉ”์„œ๋“œ์— ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง์„ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

์ถ”๊ฐ€์ ์œผ๋กœ doFilter()๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ๋งˆ์ง€๋ง‰์— ๊ผญ!!! chain.doFilter(request, response);๋ฅผ

์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

๋งŒ์•ฝ ์ž‘์„ฑํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋” ์ด์ƒ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋’ค๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š๊ณ  ๋๋‚˜๋ฒ„๋ฆฐ๋‹ค.



5. Filter ์ ์šฉ

โ’ˆ ํ•„ํ„ฐ interface๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

public class MyFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                                    throws IOException, ServletException {

            HttpServletRequest request = (HttpServletRequest) request;
            HttpServletResponse response = (HttpServletResponse) response;
            
            // ์ค‘์š”!!!
            chain.doFilter(request,resoponse);
    }
}


โ’‰ ์ƒ์„ฑํ•œ ํ•„ํ„ฐ(MyFilter)๋ฅผ Bean์— ๋“ฑ๋กํ•œ๋‹ค.

@Configuration
public class WebConfig {

    @Bean
    public FilterRegistrationBean<Filter> myFilter() {
        
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.setOrder(1);
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
}