๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐ˜์‘ํ˜•

Spring Boot ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ์ธํ„ฐ์…‰ํ„ฐ๋ฅผ ์ด์šฉํ•ด ํ—ˆ์šฉ๋œ URL๋งŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ณด์•ˆ์ด ํ•„์š”ํ•œ ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€, ๋กœ๊ทธ์ธ ์ฒดํฌ, ์ •์  ์ž์› ํ•„ํ„ฐ๋ง ๋“ฑ์„ ์œ„ํ•ด PageInterceptor๋ฅผ ์ž์ฃผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

HandlerInterceptor

public class PageInterceptor implements HandlerInterceptor {
    private static final Set<String> allowedPages = Set.of("/", "/member/login");
    private static final List<String> allowedPrefixes = List.of("/js/", "/css/", "/images/", "/favicon.ico", "/geoserver/", "/error");
    private static final List<String> allowedExtensions = List.of(".js", ".css", ".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico");

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        String header = request.getHeader("X-Requested-With");

        for (String ext : allowedExtensions) {
            if (uri.endsWith(ext)) return true;
        }

        for (String prefix : allowedPrefixes) {
            if (uri.startsWith(prefix)) return true;
        }

        if ("XMLHttpRequest".equals(header)) return true;
        if (allowedPages.contains(uri)) return true;

        response.sendRedirect("/");
        return false;
    }
}

 

WebMvcConfiogurer

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new PageInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/css/**", "/js/**", "/images/**", "/favicon.ico", "/error", "/geoserver/**", "/api/**");
    }
}

 

๋ฐ˜์‘ํ˜•