[spring] PathPattern๊ณผ servletPath
๋ฌธ์
๋ค์๊ณผ ๊ฐ์ด interceptor๋ฅผ ๋ฑ๋กํ ๋, addPathPatterns()์ /api/**๋ฅผ ๋ฑ๋กํ์์ง๋ง, /api/article์ ์์ฒญํ์ ๋ ์ธํฐ์
ํฐ๊ฐ ์ ๋๋ก ๋์ํ์ง ์์๋ค. ํ์ง๋ง /**๋ก ๋ฑ๋กํ๋ ์ ๋๋ก ๋์ํ์๋ค.
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authInterceptor)
.order(1)
.addPathPatterns("/api/**"); // not work
}
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authInterceptor)
.order(1)
.addPathPatterns("/**"); // work
}๋จ์
๋ด ์ฑ์์ application.properties์ server.servlet.contextPath=/api ๋ก ๋ชจ๋ servlet์ prefix ์ฃผ์๋ฅผ /api๋ก ์ค์ ํ ๊ฒ์ ๋ ์ฌ๋ ธ๋ค.
server.servlet.contextPath=/api์ด ๋จ์๋ฅผ ์ค์ฌ์ผ๋ก ์ฐพ์๋ณด๋ pathPattern์ ๋ฑ๋กํ ๋ ์ ์ฒด ์์ฒญ ์ฃผ์์์ contextPath๋ฅผ ๋ servletPath๋ฅผ ์ฌ์ฉํด์ผํ๋ค๋ ๊ฒ์ ์๊ฒ๋์๋ค.
๋ง์ฝ /api/article๋ก ์์ฒญํ๋ค๋ฉด contextPath๋ก /api๋ฅผ ์ค์ ํ์์ผ๋ฏ๋ก servletPath๋ /article์ด ๋์ด addPathPatterns()์ ๋ฑ๋กํ /api/**์ด ์๋ํ์ง ์์๋ ๊ฒ์ด๋ค.
์ servletPath๋ฅผ ์จ์ผํ ๊น?
๊ทธ๋ ๋ค๋ฉด ์ ์คํ๋ง์์๋ contextPath๋ฅผ ๋ผ์ ๊ฒ์ฌํ๋ ๊ฒ์ผ๊น? ๊ทธ๊ฒ์ context path์ servlet path์ ์ ์์ ์ญํ ์ด ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ด๋ค.
- context path : ์ดํ๋ฆฌ์ผ์ด์ ์ ๋ฃจํธ. ๋ํดํธ๋ โ/โ
- servlet path : prefix๋ dispatcher servlet์ ์ฃผ์๋ฅผ ์๋ฏธ(๋ํดํธ๋ โ/โ). prefix๋ฅผ ํฌํจํ ์ ์ฒด ์ฃผ์๋ ์คํ๋ง์์ ์ฒ๋ฆฌ๋๋ ์ฃผ์.
์คํ๋ง ์ธํฐ์
ํฐ๋ฅผ ๋ฑ๋กํ๊ธฐ์ํด ๊ตฌํํ ์ธํฐํ์ด์ค๋ WebMvcConfigurer์ด๋ค. WebMvcConfigurer์ ๋ค์๊ณผ ๊ฐ์ด DispatherServlet ์ดํ์ ์คํ๋ง MVC ์ค์ ์ ์ํด ์ฐ์ธ๋ค.

์ธํฐ์ ํฐ๊ฐ ์ ์ฉ๋๊ฑฐ๋(addPathPatterns๋ก ๋ฑ๋ก๋ ์ฃผ์), ์ ์ฉ๋์ง ์๋ ์ฃผ์(excludePathPatterns๋ก ๋ฑ๋ก๋ ์ฃผ์)๋ฅผ ๊ฒ์ฌํ๋ ๊ณณ์ ์คํ๋ง MVC์ด๋ฏ๋ก, ์คํ๋ง์์ ์ฒ๋ฆฌ๋๋ ์ฃผ์์ธ Servlet Path๊ฐ ๊ฒ์ฌ ๋์์ด ๋๋ค.
์๊ฐํด๋ณด๋ฉด ์ดํ๋ฆฌ์ผ์ด์ ์ ๋ฃจํธ์ฃผ์ ์ดํ๋ถํฐ ๊ฒ์ฌํ๋ ๊ฒ์ด ํ๋นํ๊ธด ํ๋ค..