Server๐Ÿงค/Spring

Servlet Filter, Spring Interceptor

yujindonut 2023. 4. 22. 17:46
728x90

Servlet Filter์˜ ํ•„์š”์„ฑ

 - ์„œ๋น„์Šค ๋กœ์ง์— ๋กœ๊ทธ์ธ ํ™•์ธ ์—ฌ๋ถ€ ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐ! 

- ์ค‘๋ณต๋˜๋Š” ์ฝ”๋“œ๋“ค

public void addCart(final Long productId, final String token) {
	final boolean isValid = jwtTokenProvider.valdiateToken(token);
    if(!isValid) {
    	throw new UnauthroizedTokenException();
    }
	final String email = jwtTokenProvider.getPayload(token);
    --- ์„œ๋น„์Šค ๋กœ์ง---
}

public List<Cart> getCart(final String token) {
	final boolean isValid = jwtTokenProvider.valdiateToken(token);
    if(!isValid) {
    	throw new UnauthroizedTokenException();
    }
	final String email = jwtTokenProvider.getPayload(token);
    --- ์„œ๋น„์Šค ๋กœ์ง---
}

 

์‚ฌ์šฉ์ž ์ธ์ฆ์„ ์ œ๊ฑฐํ•˜๊ณ  ํ•ต์‹ฌ๋กœ์ง๋งŒ์„ ๋‚จ๊ธฐ๊ณ  ์ •๋ฆฌ๋œ ์ฝ”๋“œ

public void addCart(final Long productId, final Customer customer) {
	boolean isExist = cartItemDao.existProduct(customer.getId(), productId);
    if(isExist) {
    	throw nuew DuplicatedProductInCartException();
    }
    
    Product product = productService.findProductById(productId);
    cartItemDao.addCartItem(customer.getId(), product.getId());
}

public List<Cart> getCarts(Customer customer) {
	return cartItemDao.getCartsByCustomerId(customer.getId());
}

 

์ œ๊ฑฐ๋œ ์‚ฌ์šฉ์ž์ธ์ฆ ! -> Filter๋‚˜ Interceptor๋ฅผ ์ด์šฉํ•ด์„œ ๋ถ„๋ฆฌ


Servlet Filter

Filter๋Š” J2EE ํ‘œ์ค€ ์ŠคํŽ™์œผ๋กœ Servlet API 2.3๋ถ€ํ„ฐ ๋“ฑ์žฅํ•˜์˜€๊ณ  Dispatcher Servlet์— ์š”์ฒญ์ด ์ „๋‹ฌ๋˜๊ธฐ ์ „, ํ›„์— ๋ถ€๊ฐ€ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ์ฒด. 

 

์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ

 

init()

@Override
public void init(final FilterConfig filterConfig) thorws ServletException {

}

doFilter() :  ์š”์ฒญ์ด ๋“ค์–ด์˜ฌ๋•Œ๋งˆ๋‹ค ์‹คํ–‰

@Override 
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) {

	log.info("LoginFilter.doFilter");
    HttpServletRequest servletRequest = (HttpServletRequest) request;
    try {
    	validateToken(servletRequest);
        chain.doFIlter(request, response);
    } catch (Exception e) {
    	HttpServletResponse servletResponse = (HttpServletResponse) response;
        servletResponse.setStatus(HttpStatus.UNATHORIZED.value());
    }
}

destroy()

WAS๊ฐ€ ๋‹ซํžˆ๊ธฐ์ „์— connection ํ’€์ด ๋‹ซํžŒ๋‹ค.


์„œ๋ธ”๋ฆฟ ํ•„ํ„ฐ ๋“ฑ๋ก ๋ฐฉ๋ฒ•

 

๋ชจ๋“  URL์— ์ ์šฉ๋จ

@Slf4j
@Component
public class LoginFilter implements Filter {

}

- ํŠน์ • URL์— ์ ์šฉํ•˜๊ณ  ์‹ถ์„๋•Œ 

@WebFilter(urlPatterns="")

@Slf4j
@WebFilter(urlPatterns ="/users/me/*")
public class LoginFilter implements Filter {

}

@SpringBootApplication
@ServletComponentScan
public class Application {

}

- FilterRegistrationBean 

: ์ˆœ์„œ์™€ ํŠน์ • URL ์ ์šฉ

@Bean
public FilterRegistrationBean addFilter() {
	FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
    filterRegistrationBean.setFilter(new LoginFilter(jwtTokenProvider));
    filterRegistrationBean.setOrder(1);
    filterRegistrationBean.addUrlPatterns("/users/me/*");
    return filterRegistrationBean;
}

 

 

 

ํ•„ํ„ฐ ๋™์ž‘ ๋ฐฉ์‹

 

 


Spring ์ธํ„ฐ์…‰ํ„ฐ

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

 

preHandler() : handler์ „์— ์‹คํ–‰

 

postHandler() : handlerํ›„์— ์‹คํ–‰

afterCompletion() : ์˜ˆ์™ธ์ฒ˜๋ฆฌ, ๋ฆฌ์†Œ์Šค ์ •๋ฆฌ


์Šคํ”„๋ง ์ธํ„ฐ์…‰ํ„ฐ ๋“ฑ๋ก ๋ฐฉ๋ฒ•

 

@Configuration
public class WebConfig implements WebMvcConfigurer {
	private final LoginInterceptor loginInterceptor;
    
    public WebConfig(LoginInterceptor loginInterceptor) {
    	this.loginInterceptor = loginInterceptor;
    }
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    	registry.addInterceptor(loginInterceptor)
        		.addPathPatters("/**")
                .excludePathPatters("/signup", "/login");
    }
}

ํ•„ํ„ฐ ์ธํ„ฐ์…‰ํ„ฐ
- ์ž๋ฐ” ํ‘œ์ค€ ์ŠคํŽ™ - ์Šคํ”„๋ง์ด ์ œ๊ณตํ•˜๋Š” ๊ธฐ์ˆ 
- ๋‹ค์Œ ํ•„ํ„ฐ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐœ๋ฐœ์ž๊ฐ€ ๋ช…์‹œ์ ์œผ๋กœ ์ž‘์„ฑํ•ด์ค˜์•ผํ•œ๋‹ค - ๋‹ค์Œ ์ธํ„ฐ์…‰ํ„ฐ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐœ๋ฐœ์ž๊ฐ€ ์‹ ๊ฒฝ์จ์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์ด ์—†๋‹ค.
- ServletRequest, ServletResponse๋ฅผ ํ•„ํ„ฐ ์ฒด์ด๋‹ ์ค‘๊ฐ„์— ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. (chain.doFilter๋ฅผ ํ•˜๋Š” ๊ณผ์ •์—์„œ) - ServletReqeust, ServletResponse๋ฅผ ์ธํ„ฐ์…‰ํ„ฐ ์ฒด์ด๋‹ ์ค‘๊ฐ„์— ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค.
- ํ•„ํ„ฐ์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด @ControllerAdvice์—์„œ ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ•œ๋‹ค. - ์ธํ„ฐ์…‰ํ„ฐ์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด @ControllerAdvice์—์„œ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

728x90