웹 개발을 진행하다 보면 대부분 url mapping 설정을 ant pattern으로 한다.
스프링에서 mvc:resources mapping=”/static/**” 또는 security:intercept-url pattern=”/user/**” 이 그러하다.
설정에 대한 표현식으로 ant pattern을 사용하기 때문에 *, **, ? 의 의미를 확실히 알아둘 필요가 있다.
? : 1개의 문자와 매칭 (matches single character) * : 0개 이상의 문자와 매칭 (matches zero or more characters) ** : 0개 이상의 디렉토리와 파일 매칭 (matches all files / directories)
예제 >>
import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import org.junit.Test; import org.springframework.util.AntPathMatcher; public class AntStylePatternMatcherUtilTest { @Test public void antStylePatternTest() { // double asterisks assertThat(true, is(checkAntPattern("/static/**", "/static/a.jpg"))); assertThat(true, is(checkAntPattern("/static/**", "/static/css/a.css"))); assertThat(true, is(checkAntPattern("/static/**", "/static/js/a.js"))); assertThat(true, is(checkAntPattern("/static/**", "/static/img/a.jpg"))); assertThat(true, is(checkAntPattern("/static/**", "/static/a/b/c/d/e/f/g/a.jpg"))); assertThat(true, is(checkAntPattern("/static/**", "/static"))); assertThat(true, is(checkAntPattern("/static/**", "/static/"))); // single asterisks assertThat(true, is(checkAntPattern("/static/*", "/static/a.jpg"))); assertThat(true, is(checkAntPattern("/static/*", "/static/namkyuProfilePicture.jpg"))); assertThat(false, is(checkAntPattern("/static/*", "/static/a/test.jpg"))); assertThat(false, is(checkAntPattern("/static/*", "/static/a/b/c/d/test.jpg"))); assertThat(true, is(checkAntPattern("/static*/*", "/static/test.jpg"))); assertThat(true, is(checkAntPattern("/static*/*", "/static1/test.jpg"))); assertThat(true, is(checkAntPattern("/static*/*", "/static123/test.jpg"))); assertThat(true, is(checkAntPattern("/static*/*", "/static-123/test.jpg"))); assertThat(true, is(checkAntPattern("/static*/*", "/static~!@#$%^&*()_+}{|/test.jpg"))); assertThat(false, is(checkAntPattern("/static*/*", "/static12/a/test.jpg"))); assertThat(false, is(checkAntPattern("/static*/*", "/static12/a/b/test.jpg"))); // double and single combine assertThat(true, is(checkAntPattern("/static*/**", "/static/a.jpg"))); assertThat(true, is(checkAntPattern("/static*/**", "/static1/a.jpg"))); assertThat(true, is(checkAntPattern("/static*/**", "/static/a/a.jpg"))); assertThat(true, is(checkAntPattern("/static*/**", "/static/a/b/a.jpg"))); assertThat(true, is(checkAntPattern("/static*/**", "/static/a/b/c/a.jpg"))); assertThat(true, is(checkAntPattern("**/static/**", "a/static/a/b/c/a.jpg"))); assertThat(true, is(checkAntPattern("**/static/**", "a/b/static/a/b/c/a.jpg"))); // question-mark assertThat(true, is(checkAntPattern("/static-?/**", "/static-a/a.jpg"))); assertThat(true, is(checkAntPattern("/static-?/**", "/static-a/b/c/a.jpg"))); assertThat(true, is(checkAntPattern("/static-?/*", "/static-a/abcd.jpg"))); assertThat(true, is(checkAntPattern("/static-?/???.jpg", "/static-a/abc.jpg"))); } private boolean checkAntPattern(String pattern, String inputStr) { return matches(pattern, inputStr); } public static boolean matches(String pattern, String inputStr) { AntPathMatcher antPathMatcher = new AntPathMatcher(); return antPathMatcher.match(pattern, inputStr); } }